Reputation: 2168
jmeter ForEach controller can be used to iterate over variables with same prefix like,
myVar_1
myVar_2
myVar_3
But in my case input variable is array of strings, [ "val1", "val2", "val3" ]
How to iterate over an array and send separate request for each value?
Upvotes: 5
Views: 8647
Reputation: 1
I tried Dmitri's answer, but basically got stuck with the groovy script as it works only for a simple array of strings. I, however, needed a more complex array of JSON objects. Then I switched the scripting language to ecmascript and based on the original script wrote a good old JS like this:
var jsonObject = JSON.parse(vars.get("ReportSources"));
for(var index = 0; index < jsonObject.length; index++) {
vars.put("rs_" + (index + 1), JSON.stringify(jsonObject[index]));
}
It worked for me.
Upvotes: 0
Reputation: 2168
Same way as you use for same prefixed variables.
For variable myVar
myVar = ["val1", "val2", "val3"];
//Following variables are automatically created
myVar_1 = "val1";
myVar_2 = "val2";
myVar_3 = "val3";
ForEach controller will be used on myVar_1, myVar_2, myVar_3
Use Debug Sampler to ensure.
jmeter version : 3.1 r1770033
Upvotes: 0
Reputation: 168082
You won't be able to feed this JSON Array to the ForEach Controller, but you can convert it into a form which can be understood by the ForEach Controller
Put the following code into the "Script" area:
def json = new groovy.json.JsonSlurper().parseText(vars.get("yourInputVariable"))
def counter = 1
json.each {
vars.put("myVar_" + counter, it)
counter++
}
Replace yourInputVariable
with the actual name of the variable holding the JSON Array
myVar_1, myVar_2,...
- it will work fine as JSR223 Sampler creates the relevant variables basing on the data from the JSON Array. See Parsing and producing JSON - Groovy and Groovy Is the New Black articles for more information.
Upvotes: 7