Skip to content

If Action

if-action

The If Action allows you to control whether its children are run or not. It allows your virtual users to continue under certain conditions or to use conditional branching.

The condition can be written with the old syntax as depicted on this screen shot. Or the new one when the variable expression check box is activated. We will use the new syntax in our examples since that is the recommended way.

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: If 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:

If example

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.

Conditions

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 inferior to right value
>= Left value must be superior or equal 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

Deprecated syntax

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 number 42,
  • 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".

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:

If variable expression

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 check box.

This is the recommended mode in JMeter since version 2.3.3.

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.

Deprecated Recommended
${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"))}