28

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?

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142

9 Answers9

36

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

"${my_variable}"=="foo"
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
21

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:

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • Thanks. Other things I noticed: Above expression works with or without quotes. To compare strings, user defined variable value must be double quoted. If unquoted boolean used (true/false) in UDV, the variable can be used by itself as the condition (i.e. MY_VAR=true, if ${MY_VAR} then) – Ben Flynn Apr 18 '11 at 13:09
  • Actually I took my example code from a working project, where I check for the string '1'.But maybe it's because it's a char that it works with single quotes? – Gerrie Schenck Apr 18 '11 at 19:16
  • 9
    OK, more info on String matching. Suppose MY_VAR = apples. ${MY_VAR} == "apples" # => returns false. "${MY_VAR}" == "apples" # => returns true. Suppose MY_VAR = "apples". ${MY_VAR} == "apples" # => returns true. "${MY_VAR}" == "apples" # => returns false. If you set a variable in a BeanShell, vars.put("MY_VAR","apples"); then you are in first case. You could do vars.put("MY_VAR","\"apples\""); but you're probably better off just putting the variable name in quotes for an If Controller string comparison. – Ben Flynn Apr 19 '11 at 15:45
13

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";
blogme4u
  • 229
  • 3
  • 4
12

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.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
8

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

Using the quotes was my solution.

5

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.

Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
3

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

Carlos
  • 160
  • 5
1

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

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

Siva
  • 113
  • 4
  • 13
0

Check the image

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

Finwe
  • 6,372
  • 2
  • 29
  • 44