JMeter: How to change user defined variable value for second iteration of loop count

DanzerZoneJS picture DanzerZoneJS · Sep 25, 2014 · Viewed 20.6k times · Source

I'm running a Thread Group with the following property values:

Number of threads: 200 Ramp-up Time (sec): 20 Loop-count: 2

I also have user defined variables set for the HTTP Requests. However, when the second iteration is reached, I need the user defined variable's value to also change.

Answer

Dmitri T picture Dmitri T · Sep 25, 2014
  1. Add a Beanshell PreProcessor as a child of first request
  2. Put the following code into PreProcessor's "Script" area:

    if (vars.getIteration() == 2) {
        vars.put("myVar", "newValue");
    }
    
  3. Replace myVar with your variable name and newValue with variable value for the second loop.

vars is a shorthand for JMeterVariables class instance and getIteration() method returns current loop's number.

If you want to dive deaper into Beanshell in particular and extending JMeter test via scripting in general I would recommend to get familiarized with How to use BeanShell: JMeter's favorite built-in component guide.