Abhishek Sharma
Abhishek Sharma

Reputation: 31

Jmeter - Accessing Regex Array Variables In Beanshell

I have problems picking up variables set by the Regular Expression Extractor in a Beanshell.

  1. I have an HTTP Request sampler which returns a list of 50 numbers in random form (4, 2, 1, 3....50, 45) which I have extracted via regEx.
  2. Now I want to get each and every numbers in a variable so I used again regEx with expression (.+?)(,) on JMeter variable from step# 1 above.
  3. I have problem here at this step when I am using BeanShell to access these values

Wasn't very sure I used below:

long var1 = Integer.parseInt(vars.get("Number_i"));
print("Value of var1: " +var1);

Practically I want to do this:

for (i=0; i<50; i++) {
  if (var1==1) {
    do this
  }
}

I am not adept at Jmeter, so please bear with me.

Upvotes: 2

Views: 3804

Answers (1)

Dmitri T
Dmitri T

Reputation: 168092

Given you extract variables using Regular Expression Extractor and you have > 1 match you already have multiple variables, you can check them using Debug Sampler and View Results Tree listener combination

Debug Sampler

So you can access variables in JMeter like:

${number_1}
${number_2}

and in Beanshell test elements using vars shorthand which stands for JMeterVariables class instance like:

vars.get("number_1");
vars.get("number_2");

Example code which will iterate all the matches and "do something" when current variable value is "1"

int matches = Integer.parseInt(vars.get("number_matchNr"));

for (int i=1; i<=matches; i++) {

    if (vars.get("number_" + i).equals("1")) {
        log.info("Variable: number_" + i + " is 1");
        // do something
    }
}

Beanshell compare variables

See JMeter API - JavaDoc on all JMeter classes and How to Use BeanShell: JMeter's Favorite Built-in Component for more information on how to get started with Beanshell in JMeter

Upvotes: 3

Related Questions