If Action¶
The If Action allows you to control whether the actions below it (its children) are run or not.
Warning
Be careful since only children of the If Action will be impacted. Use the indentation to check that your If Action is correctly setup.
Check condition¶
A common example is to control if the login is ok before running the rest of the script.
This can be achieved in combination with a Regexp Action:
Tip
You can download a JMX of this example here and import it in your project using the JMeter project option.
Conditional branching¶
Another example is conditional branching, typically to do either a login or an account creation before proceeding:
In this example we also need a random variable named isLogin
which is between 1 and 100.
Tip
You can download a JMX of this example here and import it in your project using the JMeter project option.
Parameter name | Description | Required |
---|---|---|
Name | Descriptive name for this container which is indicated in the bench report. The name is not required, but it is a good habit to name your containers so that you can differentiate them in your bench reports. | No |
Condition | This condition must evaluate to "true" in order to process the children of the If action. See below for more details. | Yes |
Conditions¶
The if Logic Actions can contain condition expressions. These expressions are composed of:
- variable references like
${my_variable}
, - constants such as the
"test"
string of the number42
, - comparison operators like
<
or>
, - boolean operations
&&
or||
.
E.g. you declared two variables count
and categoryId
, you may use the condition: ${count} < 10 && "${categoryId}" == "abc"
.
This condition will be true when count
value is strictly inferior to 10 AND categoryId
is equal to the "abc" word.
Tip
If you compare numeric values simply write ${myNumber} >= 100
. If you compare strings, you must enclose your variable in quotes: "${myString}" == "sampleString"
.
Comparison operators¶
Operator symbol | Description |
---|---|
== | Left value must be equal to right value |
!= | Left value must be different than right value |
<= | Left value must be inferior or equal to right value |
>= | Left value must be superior or equal to right value |
< | Left value must be inferior to right value |
> | Left value must be superior to right value |
Boolean operators¶
Operator symbol | Description |
---|---|
&& | Left value AND right value must be true |
|| | Left value OR right value must be true |
Variable expression¶
Conditions as we've seen them so far are not well optimized when it comes to running a lot of load.
In this case it is preferable to use variable expressions
:
When this checkbox is not activated, any condition used is evaluated in javascript by the IF controller.
This can be inefficient for complex conditions and large load tests.
In which case it is better to use a language like groovy AND activate the variable expression
.
Warning
Make sure to activate this option when you write the condition as a groovy script. Otherwise the result of the groovy script will be going through a javascript interpreter anyway.
Condition | Variable expression |
---|---|
${isLogin} < 30 | ${__groovy(${isLogin} < 30)} |
${loginOK} != "NotFound" | ${__groovy(!"${loginOK}".equals("NotFound"))} |
${count} < 10 && "${categoryId}" != "abc" | ${__groovy(${count} < 10 && !"${categoryId}".equals("abc"))} |
Common mistakes¶
Condition | Should be |
---|---|
${cart_status} != "PROCESSING" | "${cart_status}" != "PROCESSING" |
${loginOK} != "NotFound" | "${loginOK}" != "NotFound" |
${myvar} <= "10" | ${myvar} <= 10 |
"${loginOK}" != "Notfound" | "${loginOK}" != "NotFound" with an uppercase 'F' |
${login_${count}} != "NotFound" | ${__V(login_${count})} != "NotFound" |
${counter}<=5 | ${__groovy(${counter}<=5)} |
${__groovy(!${welcome}.equals("NotFound"))} | ${__groovy(!"${welcome}".equals("NotFound"))} |