Search code examples
javajmeterbeanshell

JMeter - Set a dynamic variable for Loop Controller


I have the following Setup:

| Thread Group
| -- BeanShell PreProcessor
| -- Loop Controller

In The BeanShell PreProcessor I have:

vars.put("myDynamicNumber", someDynamicallyGeneratedNumber+"");

In The Loop Controller I have:

Loop Count: ${myDynamicNumber}

I was under the impression this would work. Instead the Loop Controller cannot find the variable. Am I doing something incorrectly?


Solution

  • You need to use Beanshell Sampler, not Beanshell Pre-Processor.

    I don't know why does JMeter allow putting Beanshell Pre-Processor at the same level as loop controller, it should go as a child of i.e. HTTP Request.

    I'd also suggest to slightly change your code to be:

    vars.put("myDynamicNumber", String.valueOf(someDynamicallyGeneratedNumber));
    

    Which is more correct from Beanshell perspective.