DDHOOGE
DDHOOGE

Reputation: 1

Jmeter : random number with beanshell

I want to add 2 kind of parameters to a post request.

From an http request I extract 2 list of variables:

Order_id = input type="hidden" name="(drag.+?)" value="(\d+?)"

Weight_id = class="draggableviews-weight form-select" id=".+?" name="(drag.+?)"

In the Post Request that follows, I need to repost all this variables. I use a BeanShell PreProcessor for this. The first list is no problem, since this get the same value. The second list should get a new random value between -50 and 50. I also want a different random value for each iteration.

How should I do this ?

image1 image2 image3

If I use Random Variabele Config Element I get the same random int for each variabele. I want a different one for each iteration.

enter image description here

Upvotes: 0

Views: 9050

Answers (2)

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

To retrieve multiple values from single Regular Expression Extractor, we use Templates as follows:

enter image description here

then refer groups as follows: enter image description here

In the image, you can see that link is Reference Name and there is only match found with the regular expression.

number of matches is : link_matchNr
first group value    : link_1_g1
second group value   : link_1_g2

Note: The regular expression I tried on is google.com, you can also simulate the same as follows: enter image description here

  1. Use Random function as follows:

    value2 = ${__Random(-50,50)};

    log.info("valuee2 " + value2);

    1. Use Random Variable: enter image description here then, refer Output Variable in Beanshell Preprocessor as follows:

    value2 = vars.get("randomInt");

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168152

I would recommend using ThreadLocalRandom.nextInt() method like

sampler.addArgument(name2, String.valueOf(java.util.concurrent.ThreadLocalRandom.current().nextInt(-50, 51)));

Don't inline JMeter Functions into Beanshell scripts, either pass them via "Parameters" section and refer via Parameters or args shorthands or use appropriate code-based substitutions.

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter tests.

Upvotes: 5

Related Questions