JMeter for each controller - beanshell sampler display multiple variables - foreach

I have JSON Request where multiple products nodes returns from there I am using product SKU inside the for each controller as input and sku_value as output which I am using in subsequent request.
Inside this for each controller I have another beanshell sampler where I would like print/display to use other values returns by first JSON request i tried this
Jmeter counter function value to be displayed in Benashell Post Processor or sampler
vars.get("productId_" + vars.get("counter")) but it return NULL
if I use ${__V(productId_${__counter(TRUE,)})} at the sampler name it correctly prints the value but i want this value to printed using log.info
please help.

Double check that ${counter} variable exists and has valid numeric value using Debug Sampler and View Results Tree listener combination
If your ${__V(productId_${__counter(TRUE,)})} works fine you can just use it in Beanshell Sampler's "Parameters" section and refer the value as Parameters in the script:
Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy on next available opportunity.

Related

Postman: How to specify variables values in body like it is done with path params?

Postman lets you specify path variables which allow you to input variable value on the go in a small table while consuming a GET route.
Is there any way to achieve the same for variables specified within the body? So that I don't have to update environment variables every time I am trying to consume a POST/PATCH/PUT route with different data in the body.
You could set the (environment) variables in the pre-request script programatically, and then use them in the body:
pm.environment.set("variable_key", "variable_value");
Please check out Using variables for more details.

JMeter: How can I capture the variable value which is dynamic in url?

We have a URL https://www.mylink.com/est?myId=4d22b9d0-4ff2-46c3-9343-945304dfea93
The above request also contains post data:
myId=4d22b9d0-4ff2-46c3-9343-945304dfea93
How can I parameterize this as the url and post data both contain same value but its dynamic. Also, how may I store it in a variable to use it somewhere else as well?
In order to be able to use it in URL and request body you need to extract it somewhere somehow, this dynamic ID is probably associated with this or that user account so my expectation is that after logging in you should see this ID somewhere in the response.
The process is known as correlation and there is a plenty of information over the web about handling dynamic requests in JMeter. There are also solutions which provide semi or fully automated correlation of the dynamic parameters like Correlations Recorder Plugin for JMeter or JMeter Proxy Recorder
If you just need to generate an unique GUID-like structure - you can do it using __UUID() function

How to set Postman dynamic variable and use value in the same request?

I need to use the same value into two different elements/attributes on the one XML request body. I tried to add it as a collectionVariable and recall it from there but Postman generates two different values for them.
For example, I am trying to generate $randomEmail dynamic variable and use it in two request body elements:
<Email>{{$randomEmail}}</Email>
<ConfirmEmail>{{$randomEmail}}</ConfirmEmail>
When I check the sent request then I can see that Postman actually sent different values for mentioned elements like:
<Email>test1#email.com</Email>
<ConfirmEmail>test2#email.com</ConfirmEmail>
Do you have any idea how to define one specific value per each request and use it on several body elements/attributes?
store the value in a variable using prerequest script:
pm.variables.set("email",pm.variables.replaceIn("{{$randomEmail}}") )
now in the body use it like :
<Email>{{email}}</Email>
<ConfirmEmail>{{email}}</ConfirmEmail>

How can we capture access_token, session_id values if no request is showing such values in response using jmeter?

We have a portal whose POST request parameters are:
access_token, session_id,state
But I don't see any response containing values for the above mentioned parameters at all. Is there any way I can capture it?
If you don't see the values in the response body, the following options remain:
The values are in the URL, i.e. you're redirected at least once and the redirect target contains the values you're looking for in its URL
The values are in the response headers
both locations can be expected using View Results Tree listener and both locations can be queried using i.e. Regular Expression Extractor or Boundary Extractor
The values might come as the result of an AJAX request, JMeter is not a browser and it doesn't execute JavaScript so you will need to manually simulate these calls using individual HTTP Request samplers
The values might be calculated by JavaScript on browser side, if this is the case you will need to replicate the logic using JSR223 PreProcessor and Groovy language

How to save response in a variable in jmeter

I am performing load testing on my server using jmeter.
In one of my post requests, I receive a unique id in the response.
I need to send this id as a parameter in the following post requests.
I am new to jmeter and don't have any idea how to do this.
Help would be really appreciated.
If you need to store the whole response into a variable - take the following steps:
Add Beanshell PostProcessor as a child of the request which returns response you're looking for
Put the following line into the PostProcessor's "Script" area:
vars.put("response", new String(data));
Refer extracted value as ${response} where required
See How to Use BeanShell: JMeter's Favorite Built-in Component guide to lean more about Beanshell scripting in JMeter
Alternatively you can do the same with the Regular Expression Extractor, in that case relevant configuration will be:
Reference Name: response
Regular Expression: (?s)(^.*)
Template: $1$
If you need a part of response, not the whole response you can amend Regular Expression according to your needs as per Regular Expressions chapter of JMeter's User Manual
If you really need to store the whole response into a variable, do the following:
Add JSR223 PostProcessor as a child of the request which returns response you're looking for
Put the following line into the "Script" area:
vars.put("response", prev.getResponseDataAsString());
Use then this response as ${response} where you need it
But you rarely need to use the whole response and you should avoid it for big , in this case it is much better to use the Extractor that suits your response format:
JSON Extractor for JSON
CSS/JQuery Extractor for HTML extraction
XPath Extractor for XML
Regular Expression Extractor for all of them or any textual format
You can use JMeter's Post-Processor Regular Expression Extractor to extract the required value from response. Just Add this under the sampler whose response will contain the required value.
In Reg expression extractor, you will define the variable name (referenceName), RegularExpression, template etc. Later you can use the value in this variable. To learn how to use Reg expression extractor you can refer to following tutorial.
https://docs.blazemeter.com/customer/portal/articles/1743642-using-regex-regular-expression-extractor-with-jmeter
I know this question is old but I agree with #UBIK you should probably use a JSON extractor. I am working with a load test that is sending over 100 requests per second and I need to reuse the value in a specific JSON key, so I'm using a JSON extractor and saving the values in a .csv file to be used by the next request.
extract the JSON
This is the Groovy script to write it to a .csv file
def myRandomIds = new File("randomIds.csv")
myRandomIds << vars.get("id") + ","
myRandomIds << System.getProperty("line.separator")
log.info(vars.get("id") + " saved to randomIds.csv...")
This is the CSV data config I have set up in my other request that is reading from the csv file. (In my case these .jmx files are automated and parametized using jenkins)
CSV data set config

Resources