Search code examples
jmeter

Do Jmeter user variables always execute even when wrapped in an IF controller?


I have a simple representative jmeter script pictured below but in reality my real script is much more complex. Im using the latest jmeter version. I want to be able to choose a User Defined Variables script conditionally. If I set ENV variable in env_switch to local, test, or stage the result for SELECTED_ENV should match. What I observe though when I run the test (last screenshot):

  • only the Debug Sampler (test) runs proving that my if controllers are working correctly. If stage ran I would see Debug Sampler (stage) in the results tree
  • SELECTED_ENV is incorrectly set to stage at the end. It proves that even though the stage IF controller evaluates to false - jmeter still runs the user defined variable under the stage IF controller (wtf)

So it seems that even though only one of the if branches runs jmeter still evaluates all user defined variable regardless of where they are placed in the tree. The last one that runs seems to be selected. Experimentally if I place the local controller at the bottom then SELECTED_ENV will be local. Is it a bug that user defined variables ignore if controllers? Is there any workaround to do what I want to do?

An alternative I guess is just to disable the other environment scripts but its much simpler in a larger complex script to have this controlled in the way I have pictured below. Keep in mind that I really have a bunch of user defined variables so switching environments I will have to enable/disable several places and it's easy to miss one by mistake.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


Solution

  • You don't seem to understand the concept of User Defined Variables, take a look at the following lines from documentation:

    The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.

    Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.

    and especially

    UDVs should not be used with functions that generate different results each time they are called. Only the result of the first function call will be saved in the variable.

    If you need to conditionally set a variable inside this or that If Controller you should go either for Set Variables Action plugin or for JSR223 Sampler instead, the relevant JMeter Groovy code would be as simple as:

    vars.put('SELECTED_ENV', 'stage')
    

    enter image description here