Generating function value within variable in Jmeter - jmeter

I want to use the Random function within the variable in Jmeter for example
${demo_variable_g${__Random(1,4,)}}
I am extracting the values for demo_variable using regular expression extractor. I want to generate random number while using the extracted variable value. How do I do that?

The correct function would be:
${__V(demo_variable_g${__Random(1,4,)})}
From the __V() function documentation
For example, if one has variables A1,A2 and N=1:
${A1} - works OK
${A${N}} - does not work (nested variable reference)
${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1
See Here’s What to Do to Combine Multiple JMeter Variables article for explanation and demos

Related

How to generate Random number in Jmeter with prefix and store it into variable

I am using JMeter's Function Helper Dialogue and i found below syntax -
Syntax - ${__Random(12,99,customer-id)}
Result - random values b/w 12 to 99 getting generated, which will get stored in variable "customer-id"
Now Problem is I have to generate value with prefix 'test' (say-test12) and store it in variable 'customer-id'
How to do that ?
I don't see why would you need this because JMeter Variables can be normally concatenated with plain text so if you need test12 just use test${customer-id} statement where required.
However if you really need to generate some static text followed by a random number you could go for __groovy() function configured like:
${__groovy('test' + org.apache.commons.lang3.RandomUtils.nextInt(12\,99),customer-id)}
Demo:
More information:
RandomUtils JavaDoc
Apache Groovy - Why and How You Should Use It

JMeter retrieve value of value

I am using jmeter to test ldap.
As part of my test, I want to search for a random uid on each iteration. I did not find a straight forward answer to this. So my idea was to first select a random number 1-200 and saving that number as a variable named uid, that number would correspond to a UDV name.
For example
uid = 2 and 2 = A123456
in my udv list. However when trying to reference this variable in my ldap search filter.I am trying to use
(uid=${${uid}})
in hopes to get the value of the value of uid. However the search results just show this as a string.
<searchfilter>(uid=${${uid}})</searchfilter>
Is there another way to achieve what I am looking for?
Use __V function
${__V(${uid})}
The V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references
${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1
Perhaps the easiest would be going for __RandomFromMultipleVars() function?
Another option is using __V() function, it can combine and evaluate JMeter Variables

How to increase int value obtained from previous response

I'm using jmeter and I would like to automate below scenario :
(In general I would like to increase value, I already know how to extract value from previous request)
Execute request_1
Extract value1 from request_1 using Regular expression extractor
Increment value1.
Put new value (increased) to the request_2
Any idea how can I achieve it ?
Check out __intSum() function, you can sum an arbitrary number of arbitrary integers via it.
Given you have a JMeter Variable called yourVar where the extracted value lives, the relevant __intSum() function configuration to increment ${yourVar} value by 1 will be something like:
${__intSum(${yourVar},1,yourVar)}
Demo:
If the value you're getting from the Regular Expression Extractor is more than 2 147 483 647 you will need to use __longSum() function instead.
See Apache JMeter Functions - An Introduction guide for more information on JMeter functions concept.

Assigning a variable to another variable in JMeter

I have assigned a variable ${WH} to 22 and now want to assign ${WH} to another variable called ${W_ID}. I have tried a couple of things, but none of them seem to be working.
None of below working (used in User Defined variable page):
W_ID=${__eval(${WH})}
W_ID=${__evalVar(WH)}
W_ID=${__V(${WH})}
I can't figure out why the value doesn't get stored in W_ID. How can this be done?
You can add JSR223 Sampler language can be Javascript and write:
vars.put("WS_ID", vars.get("WH"))
This will move WH value to WS_ID variable.
There's an issue if you are using multiple User Defined Variables, later assignment can override previous assignment, try to avoid it. The reason can be found in manual:
Note that all the UDV elements in a test plan - no matter where they
are - are processed at the start.
You can use the __groovy() function available since JMeter 3.1.
The relevant Groovy expression which will read ${WH} variable value and save the result into ${W_ID} variable will look something like:
${__groovy(vars.get('WH'),W_ID)}
Demo:
You can put the function anywhere in your script. See Functions and Variables JMeter User Manual chapter for more information.

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 ?
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
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.
To retrieve multiple values from single Regular Expression Extractor, we use Templates as follows:
then refer groups as follows:
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:
Use Random function as follows:
value2 = ${__Random(-50,50)};
log.info("valuee2 " + value2);
Use Random Variable:
then, refer Output Variable in Beanshell Preprocessor as follows:
value2 = vars.get("randomInt");

Resources