Search code examples
loopsjmeterbeanshell

JMeter BeanShell - Loop through values


I try to use variables from an array as Path for HTTP Request. Path should be like this: mypath/${act_value}

String[] numbers = mylist.split(",");
String act_value;

for (int i = 1; i <= 25; i++) {
    vars.put(numbers[i], act_value);
}

mylist is given as output of an extractor, comma separated string: mylist=123,456,343,909

HTTP Request state cannot access to this variable, I get error:

java.net.URISyntaxException: Illegal character in path


Solution

    1. Amend your code to look like:

      String myList = "123,456,343,909";
      String[] numbers = myList.split(",");
      for (int i = 0; i < numbers.length; i++) {
          vars.put("number_" + i, numbers[i]);
      }
      
    2. Add ForEach Controller somewhere after this script and configure it as follows:

      • Input variable prefix: number
      • Output variable name: act_value
    3. Put your HTTP Request sampler as a child of the ForEach Controller

      Your HTTP Request will be executed for each value in the myList

      JMeter Iterate Variables


    Also consider switching to JSR223 Test Elements and Groovy language, in the majority of cases valid Beanshell code will be valid Groovy code but performance will be much higher.