Jmeter : random number with beanshell - jmeter

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");

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

How to correlate different values in a RegEx with multiple matches with JMeter- Correlation Recorder?

I have a problem in JMeter that I can't figure out how to solve.
Situation:
I want to load test an ASP.NET-website.
bzm - Correlation Recorder used for recording and correlation
ScriptResource and WebResource have multiple occurrences
Now I used following RegEx to extract the ScriptResource values: <script src="(.+/ScriptResource.+?)". Then I stored the 3 matched values into 3 different variables. But I can't replace the values in the found order.
Example:
First Match: stored in : AspNet_ScriptResource
Second match stored in: AspNet_ScriptResource_1
Third match stored in: AspNet_ScriptResource_2
The three matches have all different values, can't paste one value into all 3 matches.
So I have to write the value from AspNet_ScriptResource into the first match (matchNr=1).
AspNet_ScriptResource_1 into second match (matchNr = 2) and
AspNet_ScriptResource_2 into third match (matchNr = 3).
But in the Correlation Rules (in the Correlation Recorder) in the "Correlation Replacement" section, there is no option to choose in which matchNr to write.
Correlation Recorder: Correlation Rules
I tried to use a BeanShell Preprocessor with a for-loop but I only found a way to write in the logs, not in the response body.
Is there a way to solve this with the Correlation Recorder plugin? If not, what options do I have to handle such a scenario?
Thanks for your help! :)
You can raise an enhancement request for the bzm - Correlation Recorder via JMeter Plugins support forum or if you're a BlazeMeter customer and need this piece of functionality asap - you can open a support ticket for implementation of the feature.
Currently it seems that the Correlation Recorder expects you to provide a regular expression either with the unique match or you will have multiple substitutions with the same value.
With regards to Beanshell approach:
You can substitute response data, but not with the PreProcessor, you need to do this via the PostProcessor using prev.setResponseData() function
Since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting
If you need to change request data, not the response data, you can do this via sampler.getArguments() function

JMeter - compare assersion between two variables

I have two variables (create_date) took from SQL queries, and I want to validate the results.
Using Regular Expression I fetch both created date and I want to make sure that the dates is not equal.
I have created the following BeanShell assersion to compare both variables:
enter image description here
however, I'm getting the following error:
enter image description here
You're mixing 2 approaches:
Referencing variables like ${CreateDateBefore} in scripts (which you should not be using at all)
And using vars shorthand for JMeterVariables class instance
Apart from this you're using double to store some form of a timestamp
So you need to update your code like:
String var1 = vars.get("CreateDateBefore");
String var2 = vars.get("CreateDateAfter");
Also be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion
And last but not the least, you can compare 2 JMeter Variables using "normal" Response Assertion configured like:

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.

Is it possible to include jmeter variables in values obtained from CSV?

I have a csv file which contains a column named "query". One of the entires I have for query is /user/${id}/list/${list}.
What I would like to do is let jMeter overwrite the ${list} and ${id} variables in the query when it is passed to a HTTP Sampler with variable values already in use from previous steps in my test plan.
For example:
In test plan, create ${id} = 5 and ${list} = 10.
In test plan, open csv file that contains query string.
In test plan, perform use a HTTP Sampler. Path in query should be the query value passed from csv file.
3a. Jmeter should take query passed to sampler and replace ${id} and ${list} with the values stored to those variables within test plan (5 and 10).
Right now when I try this, the HTTP response comes back showing the request was made to /user/${id}/list/${list}, not /user/5/list/10.
Does anyone know how to force the substitution through jMeter?
Is it even possible?
I was able to figure this one out after a bit of head scratching.
JMeter allows you to overload variables (place references to variables within a variable) by using the __eval function.
To get around the issue, I left the csv file as is, with references to variables set. When I wanted to reference the query from the csv file and overload the variable placeholders with actual values I used ${__eval(${query})} - where query = the
Try to use __eval function instead:
/user/${__eval(${id})}/list/${__eval(${list})}
__eval function seems to be just your case.

Resources