I have several JMeter test plans which should be executed in different environments, say Dev, Test, UAT, Live. In each test plan I would like to have a simple way to specify whi
I would suggest to substitute all environment-specific variables or values with JMeter Properties. See following functions for reference:
For example you can define a property called hostname
in either jmeter.properties file or as JMeter command line argument as follows
jmeter -Jhostname=169.140.130.120 -n -t yourscript.jmx -l yourscriptresults.jtl
and refer to in inside your script as:
${__P(hostname,)}
or${__property(hostname,,)}
See Apache JMeter Properties Customization Guide for more details.
I use a JSR223 sampler with Groovy to register new parameters. Make sure to put the JSR223 sampler in a Only Once Controller, so it is ran only once (per thread).
I have in the test plan all variables registered for all environments like this:
TEST.user = 123
TEST.url = test.mysite.com
LOAD.user = 435
LOAD.url = load.mysite.com
Then I specify which environment I want to select with a command line parameter at startup like: -Denvironment=TEST
My JSR223 script will add new variables to the test, without the TEST. prefix. In my scripts I just reference ${user}
This is the script to use:
JMeterVariables jmv = new JMeterVariables();
for (Map.Entry item : vars.entrySet()) {
if (item.getKey().startsWith(env)) {
String keyname = item.getKey().substring(env.length()+1);
String origval = vars.get(keyname);
if (origval==null || (origval.equals("") && !item.getValue().equals(""))) {
jmv.put(keyname, item.getValue());
}
}
}
if (!jmv.entrySet().isEmpty()) {
vars.putAll(jmv);
}
Like the Manish Sapariya mentioned, Parametrized Controller is quite useful to prepare configuration for more than one environment. I used it in the previous place I worked and started the configuration now in new place. It is a bit of work at the beginning, but later it is easy in maintenance. There is a bit of tutorial in the link that is provided above, but it won't take in consideration that you want to run just one env at a time. I will describe it a bit below, maybe it will be useful. So, slowly step by step:
The most important thing here is that I have them separated at this moment by prefixes in variable names, so in one UDV element I have variables like dev.serverIP, dev.serverPort, preprod.serverIP and so on (second screenshot) populated with values relevant for that environment. Additionally in one of this UDVs I have environmentType variable (mentioned earlier) with default value 'dev' (which you can change manually here or provide different value when launching through command line/CI or whatever)
Now in the Env Profiler I have If Controllers(no 4 on the first screenshot). For dev env I have (no 5 on the first screen):
"${environmentType}" == "dev"
For each env (if controller) you have to provide proper condition like this above.
And that it is pretty much it. Now maintaining:
I know it's a lot to do when you start, but it is not so bad later, when just adding some stuff - probably the easiest way to do it anyway.
I have not used myself, but this jmeter-pluing may help you. Here is snippet from documentation
Parameterized Controller since 0.1.0
When your JMeter test plan tree becomes like a sequoia or a banyan,
you start feeling yourself like a monkey in a jungle, jumping from
branch to branch, trying to support this important test consistent.
You really need some way to have parameterized subroutines, to reuse
parts of test plan like regular programming language functions and
procedures.
JMeter have out-of-box Module controller, but it has no parameters
to pass to, so if you need to call repeating sequence of the same
action with different parameters, your reflection in a mirror starts
morphing into monkey. Parameterized Controller helps you stay human
and sane.
I have the same problem. My current approach is to have several user defined variable (UDV) elements, e.g., DevVariables, TestVariables, etc. Each of these have the same variables defined (hostname, port, etc.). Then I manually disable those UDV elements that aren't in use.
Edit:
The JMeter User Manual states - "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." I'm not sure what event constitutes the "start", but it makes it sound like you can't conditionally include UDVs, at least not via controllers.
You may be able to have a single UDV per test plan, where you set the various variables (host, etc.). Maybe you can set each variable's value using an ugly JavaScript function that keys off of a passed-in property value.
You might try asking the question on the Apache JMeter User mailing list. There must be a way to do what you want.
As the current solution I'm using JSR223 sampler with custom JavaScript code to set up variables from external properties-files. Something like that:
var file = new java.io.File(args[0]);
var props = new java.util.Properties();
var is = new java.io.FileInputStream(file);
props.load(is);
is.close();
for(var it = props.entrySet().iterator(); it.hasNext();)
{
var entry = it.next();
vars.put(entry.getKey(), entry.getValue());
}
Now I just need to add this code as the very first sampler in a test plan and pass environment specific filepath as the sampler parameter args[0]
it will load variables from the file and put them as JMeter variables.