sarbo
sarbo

Reputation: 1691

How to add multiple values to a parameter in jMeter

How can I add multiple values (these values are extracted with regex extractor) to a parameter.

I have the following test: enter image description here

Using the regex extractor I get the following:

enter image description here

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:

enter image description here

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

Answers (1)

Dmitri T
Dmitri T

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

Related Questions