I was trying to hit Myntra's home page and search for puma in the search box using JMeter. Using the regEx extractor I extracted few values from the response as shown below:
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@1589f854
START.HMS=122825
START.MS=1532069905949
START.YMD=20180720
TESTSTART.MS=1532073140645
__jmeter.USER_TOKEN__=Thread Group 1-1
outValue=puma?f=gender:men::categories:Tshirts
value_1=puma?f=gender:men::categories:Tshirts
value_1_g=1
value_1_g0="value":"puma?f=gender:men::categories:Tshirts"
value_1_g1=puma?f=gender:men::categories:Tshirts
value_2=puma?f=gender:men::categories:Casual Shoes
value_2_g=1
value_2_g0="value":"puma?f=gender:men::categories:Casual Shoes"
value_2_g1=puma?f=gender:men::categories:Casual Shoes
value_3=puma?f=gender:men::categories:Sports Shoes
value_3_g=1
value_3_g0="value":"puma?f=gender:men::categories:Sports Shoes"
value_3_g1=puma?f=gender:men::categories:Sports Shoes
value_matchNr=3
Now using For Each Controller I can pass these values to my next HTTP request and iterate through them once as shown below:
But I want to do the same thing using a BeanShell preprocessor and am new to scripting, so I need help on this that how I can do the same using a BeanShell preprocessor and pass the values to my next HTTP request.
Suggestions are welcomed.
Use value_matchNr
to find out how many instance of the variable you have. Then loop: build an appropriate variable name, and get its value using vars.get(name)
:
// First, use the value of 'value_matchNr' to identify how many variables of type 'value_...' we have
int count = 0;
try {
count = Integer.parseInt(vars.get("value_matchNr"));
} catch(NumberFormatException e) { log.error("Variable 'value_matchNr' was not found. There won't be any looping"); }
// Next, loop through variables (if there's at least 1 to loop through)
for(int i = 1; i <= count; i++) {
String name = "value_" + i; // build variable name, e.g. value_1, value_2, etc
String value = vars.get(name); // get variable value
// at this point you can do whatever you want with the value. For example print it out:
log.info("Variable '" + name + "' has value '" + value + "'");
}