Jmeter Global Properties
Apache JMeter uses the concept of local and global variables. Typically, if a variable is inside a thread group it will only be visible within it. So if you have multiple thread groups and need to share variables across them you can use Apache JMeter properties (global variables).
A good use of properties is to create a test plan which can be used across environments. By this we mean integration, system test and pre-production.
First we create a setup thread group and under that user defined variables. We then define the specific environment variables to use across the test plan.
In each case we define:-
protocol
url
port
username
password
proxy.url
proxy.port
We also prefix each variable with:-
integration
syst
preprod.
With the user defined variables set we then need to write them to global variables (properties). In this example, we use a JSR223 sampler to put the values inside properties. We initially read in an environment variable called ENV. Depending on the value chosen, “integration”, “syst” or “preprod” we will jump inside the appropriate case statement.
Then we use props.put to assign the values from the user defined variables section and assign each to the equivalent property. e.g. props.put("protocol", "${integration.protocol}");
switch ("${__P(env,${ENV})}") {
case "integration":
props.put("protocol", "${integration.protocol}");
props.put("url", "${integration.url}");
props.put("port", "${integration.port}");
props.put("username", "${integration.username}");
props.put("password", "${integration.password}");
props.put("proxy.url", "${integration.proxy.url}");
props.put("proxy.port", "${integration.proxy.port}");
break;
case "syst":
props.put("protocol", "${syst.protocol}");
props.put("url", "${syst.url}");
props.put("port", "${syst.port}");
props.put("username", "${syst.username}");
props.put("password", "${syst.password}");
props.put("proxy.url", "${syst.proxy.url}");
props.put("proxy.port", "${syst.proxy.port}");
break;
case "preprod":
props.put("protocol", "${preprod.protocol}");
props.put("url", "${preprod.url}");
props.put("port", "${preprod.port}");
props.put("username", "${preprod.username}");
props.put("password", "${preprod.password}");
props.put("proxy.url", "${preprod.proxy.url}");
props.put("proxy.port", "${preprod.proxy.port}");
break;
}
With the properties defined we can then use them in the main thread group. In this case we are using the protocol, url and port details. We are also allowing these value to be overridden at the command line if required.
This practice is a great way of reducing the number of test assets required and ensuring that the testing is repeatable across environments.