문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top