If I set the put key value to the dynamic i value in the graph (this is a requirement),
When I get vars.get("i"), I can only get the value aaa2 when i=2
I want to get all the values(aaa1 and aaa2), what should I do?
To view multiple values you need to save multiple variables by changing your vars.put line to:
vars.put("i" + i, a);
and then get it using ${i[number]} as:
${i0} ${i1} ${i2}
Another option is to concatenate values to variable ${i] similar to
vars.put("i" , (vars.get("i") == null ? "" : vars.get("i")) + a);
Also change your Beanshell sampler to JSR223 Sampler according to JMeter Best Practices
we advise switching from BeanShell to JSR223 Test Elements
Related
Hi All / Dimitri T Could you please post your valuable thoughts on extracting corresponding Values (For example ItemID1 and ItemSlot1) in one block of code randomly. I was able to write below Jsr223 postprocessor code and it is working fine. But when there are Blank spaces in ItemSlot id, then they are not fetching. From below code , i am passing ${rannum} under "Match No" in required regular expression.
Note: There will be more than 100 corresponding values. In some cases, we won't have ItemSlot1.i.e Blank/null values are appearing from server response. Hence, my script is not picking corresponding values.
Application Server Response:
"viewSaleListingLink": "https://Example.com/cars/item/search/-/listing/ItemID1/100011142",
"saleCountry": "",
"saleNote": "",
"bidLiveUrl": "https://Example.com/cars//registration?p_p_id=RegistrationPortlet_WAR_PWRWeb&p_p_lifecycle=1&p_p_state=normal&ItemSlot1=103009468",
JSR223PostProcessor Code
import java.math.MathContext;
import java.math.RoundingMode;
// Read occurance values from pervious response
def Max = Integer.parseInt( vars.get("ItemID1_matchNr"));
int min=1;
int rannum = min + (int) (Math.random() * ((Max - min) + 1));
log.info("Values id ="+rannum);
vars.put("rannum",rannum.toString());
enter image description here
If you need to extract a random match/pair of matches from the response using Regular Expression Extractor - it's sufficient just to provide 0 as the "Match No" and it will automatically fetch the random match group so you won't have to write any code:
Also be aware that Post-Processors are executed in the order they appear (upside down) so:
If your JSR223 PostProcessor is above the Regular Expression Extractor - ItemID1_matchNr will be undefined
If your JSR223 PostProcessor is below the Regular Expression Extractor - your rannum variable won't have any value
Also your response seems to be JSON so it makes sense switching to JSON JMESPath Extractor which is more powerful and convenient
How to achieve a kind of mapping between string and numeric values that can be used for comparison in assertion? Example:
MAP "DELIVERED"=0
"PENDING"=1
"WAITING"=2
sampler1 - extracted numeric_value=0
sampler2 - assert string value="DELIVERED" is equal to its numeric value
Please check the below test plan:-
I have used variable name from regular expression of 1st sampler in the switch controller like ${regVar}..Then used the second request 3 times i.e. 0,1,2 and used response assertion with the desired value like "DELIVERED"=0 for first sampler under switch i.e "0" then in second "PENDING"=1 i.e "1"..so on.
With this, based on the regEx value from 1st http sample, only one http request will be send for 2nd sampler and that request have it own assertion. I have tried with both positive and negative cases. Please change the assertion value based on your requirements.
Please check if it helps.
Be aware of JSR223 Assertion which allows you to use arbitrary Groovy code to define pass/fail criteria.
You can access the numeric_value using vars shorthand for JMeterVariables class like:
def numericValue = vars.get('numeric_value')
Example code:
def myMap = ['0':'DELIVERED', '1':'PENDING', '2':'WAITING']
def numericValue = vars.get('numeric_value')
log.info('Numeric value is: ' + numericValue)
log.info('Status is: ' + myMap.get(numericValue))
Demo:
More information: Scripting JMeter Assertions in Groovy - A Tutorial
Thanks Dmitri, implemented solution based on your suggestion and it suites fine.
I would like to read values from a select query and use the values sequentially in a API call.
Since the database values get stored as var_1, var_2 ... var_N
So how do i increment the number for the variable?
I used a counter and pre processor to increment the number in variable
vars.put ("email", "email"+"_"+vars.get("counter"))
But the final variable is not getting replaced by the value from the select query
eg select query result from a debug sampler
email_1=hata.pd.h13u#yopmail.com
email_2=hata.pd.h13u#yopmail.com
email_3=hataiot.test13#mailinator.com
email_4=hataiot.test12#mailinator.com
--combining the variable and counter:
vars.put ("email", "email"+"_"+vars.get("counter"))
--using the variable in the API post body
{
"username":"${email}",
"password":"test1234"
}
Actual result:
POST data:
{
"username":"email_1",
"password":"test1234"
}
Expected result:
{
"username":"hata.pd.h13u#yopmail.com",
"password":"test1234"
}
TIA
${__V(email_${counter})}
Try this one, see also the documentation: http://jmeter.apache.org/usermanual/functions.html#what_can_do
Note that variables cannot currently be nested; i.e. ${Var${N}} does not work. The __V (variable) function can be used to do this: ${__V(Var${N})}. You can also use ${__BeanShell(vars.get("Var${N}")}.
Have you considered using ForEach Controller? It's super-handy for iterating the variables from extractors or JDBC test elements.
If you still want to continue with the current approach you need to change this line:
vars.put ("email", "email"+"_"+vars.get("counter"))
to this one:
vars.put ("email", vars.get("email"+"_"+vars.get("counter")))
Because you're putting into email variable stuff like email_1, email_2, etc. instead of actual ${email_1} variable value.
References:
JMeter Functions and Variables
How to Retrieve Database Data for API Testing with JMeter
I have scenario where I need to capture dynamic Order-value IDs from response and among those values, I need to select maximum value and pass it to the next request.
How can we achieve this with the help of Jmeter tool?
Assuming you have the following JMeter Variables from the PostProcessor:
foo_1=1
foo_2=5
foo_3=10
foo_matchNr=3
You can get the maximum value as follows:
Add JSR223 PostProcessor as a child of the request, make sure it goes after the PostProcessor which returns your Order-ID values
Put the following code into "Script" area:
List values = new ArrayList()
for (int i=1; i <= (vars.get('foo_matchNr') as int); i++) {
values.add((vars.get('foo_' + i) as int))
}
vars.put('foo_max', Collections.max(values) as String)
Assuming everything goes well you should be able to access the maximum value as ${foo_max} where required.
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter tests
I am trying to track the value by printing it to the log but getting void as an answer.enter image description here
The easiest way to see the variable names along with values is using Debug Sampler
However if you need to print all the extracted values to JMeter log for some reason you need to slightly change your script to look like:
log.info("Detected " + vars.get("urls_matchNr") + " URLs");
for (int i=1; i<= Integer.parseInt(vars.get("urls_matchNr")); i++) {
log.info("URL # " + i + ": " + vars.get("urls_" + i));
}
vars stands for JMeterVariables class instance so this way you get read/write access to all Jmeter Variables in scope.
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter
There are 2 problems in your script.
1) You are extracting Match No: -1, which is not right (Check your regular expression extractor). You can either chose 0 for random match or any positive number for the respective match.
2) In BeanShell assertion, you are trying to retrieve the value as
logs.info("the" +urls);- which is not the right way to do.
To get the value of a variable in BeanShell we have to use "vars.get" method.
So change your assertion to logs.info("the" +vars.get("urls")); and try once.