Reputation: 1691
How can I add multiple values (these values are extracted with regex extractor) to a parameter.
I have the following test:
Using the regex extractor I get the following:
Now I'm using a BeanShell PreProcessor that contains the following code:
int count = Integer.parseInt(vars.get("articleID_matchNr"));
for(int i=1;i<=count;i++) { //regex counts are 1 based
sampler.addArgument("articleIds", "[" + vars.get("articleID_" + i) + "]");
}
Using this will generate the following request:
This will add multiple parameters with the same name (articleIds) which will cause an error when I'm running the test. The correct form of the parameter should be:
articleIds=["148437", "148720"]
The number of articleIds is different from a user to another.
Upvotes: 0
Views: 6990
Reputation: 168072
That's totally expected as you're adding an argument per match. You need to amend your code as follows to get desired behavior:
StringBuilder sb = new StringBuilder();
sb.append("[");
int count = Integer.parseInt(vars.get("articleID_matchNr"));
for (int i = 1; i <= count; i++) {
sb.append("\"");
sb.append(vars.get("articleID_" + i));
if (i < count) {
sb.append("\", ");
}
}
sb.append("\"]");
sampler.addArgument("articleIds", sb.toString());
See How to use BeanShell guide for more details and kind of JMeter Beanshell scripting cookbook.
Upvotes: 5