27
votes

I was reading the JMeter documentation and came across this info box about "If Controllers":

No variables are made available to the script when the condition is interpreted as Javascript. If you need access to such variables, then select "Interpret Condition as Variable Expression?" and use a __javaScript() function call. You can then use the objects "vars", "log", "ctx" etc. in the script.

I don't quite follow this. Does this mean if I want access to a "User Defined Parameter" then I can access it only by writing some JavaScript? The example that follows this box then refers to "${COUNT}"

Could someone clarify the usage of the If Controller, maybe with an example or two?

9
Date.now() - ${__property(lastUpdateTimeMS)} > 900000 - djangofan

9 Answers

34
votes

All these answers are wrong! You need to put the variable reference in quotes, like so:

"${my_variable}"=="foo"
21
votes

You can simply use something like

${my_variable}=='1'

Sometimes JMeter documentation can be confusing :)

Edit 27 september 2017:

The answer here works but has a very bad performance impact when number of threads exceeds 40.

See below for correct and most performing answer:

See:

11
votes

If Controller will internally use javascript to evaluate the condition but this can have a performance penalty.

A better option (default one starting from JMeter 4, see https://bz.apache.org/bugzilla/show_bug.cgi?id=61675) is to check "Interpret Condition as Variable Expression?", then in the condition field you have 2 options:

  • Option 1 : Use a variable that contains true or false. For example If you want to test if last sample was successful, you can use

${JMeterThread.last_sample_ok}

If Controller starting with JMeter 3.4

or any variable you want that contains true/false

${myVar}

  • Option 2 : Use a function (${__jexl3()} is advised) to evaluate an expression that must return true or false. For example if COUNT is equal to 1:

${__jexl3("${COUNT}"== "1",)}

OR

${__jexl3(${COUNT}== 1,)}

If Controller with expression starting with JMeter 3.4

Starting with 4.0, if you don't use the "Interpret Condition as Variable Expression?", a warning in RED will be displayed:

If Controller using javascript in JMeter 3.4

If you'd like to learn more about JMeter and performance testing this book can help you.

10
votes

UNCHECK the CHECKBOX "Interpret condition as variable expression"

I wasted a couple of hours without unchecking this checkbox. It worked with and without semicolon(;) at the end of the statement. Make sure that you have set the User-Defined Variables before calling the if controller.

All the following variations worked for me in Jakarta Jmeter 1.5

  • ${__javaScript("${HOMEPAGE}"=="Y")}
  • ${__javaScript("${HOMEPAGE}"=="Y")};
  • "${HOMEPAGE}"=="Y"
  • "${HOMEPAGE}"=="Y";
8
votes

God bless the http://habrahabr.ru Have tried until found these. enter image description here

Using the quotes was my solution.

5
votes

As Gerrie said you need to check your variable

${my_var} == 'value'

But be careful with the 'User Defined Variables'

Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.

That basically means that you cannot define 'User Defined Variables' inside an 'If Controller'. Take a look to the 'BeanShell' instead.

3
votes

Replace: ${my_variable}=='1' with "${my_variable}" == "1"

1
votes

if it's string value pass as below and its performance effective

${__groovy("${key}"=="value")}

0
votes

Check the image

I have used ${code_g1}== 200 in condition and it worked for me.