0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Im aware of the difference between variables (thread specific) and jmeter properties (global). Unfortunately jmeter doesnt seem to have a config element to set properties or at least I have not found one. I dont like setting everything via groovy script because there are hundreds of properties and its hard to compare them without a table UI view. Really what I didnt show (for simplicity) is I have a groovy script that converts the variables into jmeter properties. It runs in a setup thread so just 1 time. So I can have a GUI to set the values and still output jmeter properties.
The plugin you mentioned does seem to work within if controllers and solves the problem. Would be nice to not have to use plugins as the scripts are shared and others need to be reminded to install the plugin but its fairly easy now with jmeter plugin manager. Thanks for your inputs!
If you read my answer to the end you will see the solution without plugins
yep I read it to the end and mentioned in my first comment that I don't like the idea of using groovy script and why. In the end I decided to move all the config to an external CSV with separate columns for each environment. Then I have a single UDV file with a property called environment. I use a CSV data set to read the CSV and pull just the correct column based on environment. But both your options would work which is why I flagged it as the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.