How can I use the value of prev HTTP Request to the next HTTP Request in jMeter? - jmeter

I'm newbie in jMeter and I'm working with it.
I have one Thread Group which consists of two Loop Controllers.
In 1st Loop Controller, there's a HTTP Request which contains body data
{ ..., "var1": "var-${__RandomString(10,1234567890abcdefg)}", ... }
In 2nd Loop Controller, there's also another HTTP Request that contains var1, and I want to asssign this var1 with same value as the prev var1 in the 1st Loop Controller.
I have tried use User Defined Variables, but it generated same value for another Thread Groups, I want generate different value of var1 for each Thread Group.
Can anyone help me what should I do to make it works? Thank you :)

Add Post Processors --> Regular Expression Extractor to the Http Request which you want to fetch the variable named var1.
As the above picture is shown use "var1": "(.+?)", as Regular Expression. The (.+?) part defines that Hey! Regular Expression Extractor, please fetch for me whatever is between "var1": " and ",, which you'll find in the response content. So var-${__RandomString(10,1234567890abcdefg)} would be fetched into a variable named Var1 (Because you defined Var1 as the Reference name.
Then, in the Http Request where you want to access that variable, use ${Var1} in order to send the value of Var1 variable as a request parameter. Something like the picture below
Note that the last value in the loop will be saved in Var1. If you want to save all values in the first loop in order to use those in the second loop,There's many way to do that.
You can add a Counter from Config Element to the first loop Controller and define for example loopCounter as the reference name for it. Fill the start and increment fields with 0 and 1.
Then change the regular expression to Var${loopCounter}
In the next loop's Http Request you can access these variables with ${Var1}, ${Var2} and ...

Just change your __randomString() function to look like:
${__RandomString(10,1234567890abcdefg,var1)}
it will overwrite ${var1} with random generated value so you'll get the same random string in both loop controllers.
See How to Use JMeter Functions articles series for more information on the above and other functions.

Related

Can a request parameter extracted from response of previous request have different values when run multiple times - Jmeter

Scenario:
We are extracting a value from 1st request and passing it as a parameter for the 2nd request.
The 2nd request is in Loop controller and it is run multiple times, But every time the 2nd request runs , It should take different value. Is there any way to do this.
Eg: Below is the example screenshots for the same. data is the variable which is passed to the second request .When second request is hit multiple times , It should extract different values.
In the Regular Expression Extractor set "Match No" to -1 to you will extract all the matches into:
data_1=1
data_2=2
etc.
In the Loop Controller set the "Loop Count" to ${data_matchNr}. This way the controller will iterate as many times as there are matches in the Regular Expression Extractor
Instead of ${data} use ${__V(data_${__intSum(${__jm__Loop Controller__idx},1,)},)}
More information: Here’s What to Do to Combine Multiple JMeter Variables

JMeter how to select randomely from a extracted set of values

I have a requirement to use randome url from a url list I extract from a json response.
Say I extract them in this mannser
imageUrls_1=https://blah01.com
imageUrls_2=https://blah02.com
imageUrls_3=https://blah03.com
imageUrls_4=https://blah04.com
imageURLs_matchNr=4
In a following JSSR223 sampler I was able to generate a variable called "url" with one of the url names selected randomely
("imageUrls_1","imageUrls_2",etc)
I was thinking to use them in my HTTP request to get the correcponding url as follows. ${${url}}. But soon found out its not giving me anything other than "${${url}}" :(.
JMeter Is it possible to place a varibale inside a varible name?
Basically I need to use one of the extracted urls randomely in my HTTP request.
The easiest way is going for __V() and __Random() functions combination like:
${__V(imageUrls_${__Random(1,${imageURLs_matchNr},)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables
Use __V function
${__V(url)}
The V (variable) function returns the result of evaluating a variable name expression.

Fill an arraylist with repeated HTTP Request's Response Data on Jmeter

I need to process a specific URL (that returns different numbers each time) and get its return into an array to use it in another HTTP Request.
My test is built like this:
Loop Controller (2 loops)
HTTP Request
JSON Extractor
Reference Name: myVar
Match No.: -1
Compute concatenation var: checked
Debug Sampler
When this block is executed, when I check the Debug Sampler's Response Data, it always fills the variable myVar with the last occurrence, like this:
myVar_1=
myVar_1_g=0
myVar_1_g0=3154
myVar_matchNr=1
Shouldn't it also store something like myVar_2, since I defined the "Match No.:" as **-1 **?
I read some answers as this one and this but my problem is that I need to iterate a variable with some response data, no matter how many times it is executed.
The reason is that on each iteration of the Loop Controller your myVar variable gets overwritten
If you want to store myVar variable value for each loop you should amend its reference name to look like:
myVar${__jm__Loop Controller__idx}
Then given you have 2 iterations of the Loop Controller you will have the following variables defined when the loop ends:
myVar0=xxxx
myVar1=yyyy
The ${__jm__Loop Controller__idx} variable is available since JMeter 4.0

JMeter: Can't copy CSV variable into another variable

I am reading a token from a .csv file into variable CSV_ACCESS_TOKEN. I have 3 request under one ThreadGroup. I want a scenario when logged in user loads a page thrice (or N time). So 1 thread is looping N time. After reading token once, I dont want to read next token in loop but want to loop through URL three (or N) time with same token.
Right now I am reading data from CSV, and using "BeanShell Sampler" inside "Once only Controller". In the sample I am using line like: vars.put("ACCESS_TOKEN",vars.get("CSV_ACCESS_TOKEN"). But that BeanShell sampler is recorded in my Summary Result. I don't want that.
I tried using "User Defined Variable" controller and try to assign the value ${__evalVar(CSV_ACCESS_TOKEN)} but it return empty value for ${ACCESS_TOKEN}. When I use ${CSV_ACCESS_TOKEN}, it shows me the values. If I use some other variable instead of CSV_ACCESS_TOKEN in UDV controller, it assigns the value from other variable and I see values for ${ACCESS_TOKEN}.
Why CSV variable is not assigning the values in regular variable.
Thanks
Vinay
If you have 3 requests, I suggest you put a Beanshell preprocessor on the first request, which copies the CSV_ACCESS_TOKEN to ACCESS_TOKEN.
Each of your samples can the use ACCESS_TOKEN, so CSV is accessed once per cycle of 3.
Each time the preprocessor run (ie before each 1st request), CSV_ACCESS_TOKEN will get updated from the dataset.
If it is the same request, which you do not wish to duplicate, you can look into use of test fragments and modules, so you can run the same sample from a variety of controllers. First from a simple controller with the preprocessor attached, and then from a loop controller to perform 2 more requests.
I think the code you have used already to manipulate the CSV values will continue to work in this scenario.

Modify a Property on JMeter

I have created a Test Plan that has two thread groups. Each thread group has a SOAP/XML-RPC Request Sampler. Thread Group A also has a Regular Expression Extractor that contains:
Reference Name : ABC
Regular Expression :<response>([A-Z 0-9]+)</response>
Template: $1$
Moreover, Thread Group A has a BeanShellAssertion with
Name: Extract value
Script: ${__setProperty(ABC, ${ABC})};
What I want to do is modify the ABC variable and then pass it on the SOAP Sampler of the second Thread Group.
So, if ABC equals 1000 (response tag holds an int) I want to get that value divided it by two and then pass it on the second sampler like :
<abcValue>${__P(modifiedABC)}</abcValue>
Any ideas?
EDIT:
I have tried preProcessors(on the second thread group) and postProccessors (on the first thread group) but whatever I tried gave me back errors like:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String value = Integer.toString(Integer.parseInt(72295) /2); props.put("modifiedABC", v . . . '' : Typed variable declaration : Error in method invocation: Static method parseInt( int ) not found in class'java.lang.Integer'
If your response contains anything other than A-Z, ' ', or 0-9, the regex extractor will fail. It may be better to set the match group to (.+), so it collects whatever is in the response and use a separate regex assertion to check the contents are what you expect. That way you will get a sample fail when the results are bad, rather than a subsequent fail, when your next sample is badly formed through bad input.
In the Thread Group A assertion, you need some "s..
${__setProperty("ABC", "${ABC}")}
This sets a property called ABC to the value of variable called ABC, which is what I think you intend.
Easy way to divide your value is with __javaScript() function..
${__javaScript(${ABC}/2)}
You can use this anywhere in jmeter and it will substitute the value you require. Make sure you have retrieved the property at the start of Thread Group B, as the variable (ABC) is in different scope.
Thread Group 1
Please use 'Beanshell Post Processor' for your request. Add this post processor under the sampler where you extract ABC.
Below link will you give you an idea.
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor
Note that by default everything is String in Jmeter. So you might want to convert it to an Integer before dividing it by 2. You have to add something like this in the Beanshell Post processor.
modifiedABC = Integer.toString(Integer.parseInt(props.get("ABC"))/2);
props.put("modifiedABC",modifiedABC);
Thread Group 2
Now you want to access the modifiedABC in the second thread group.
Simply access it using
${__P(modifiedABC)}

Resources