30

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?

1
  • Date.now() - ${__property(lastUpdateTimeMS)} > 900000 Commented Aug 17, 2015 at 17:41

9 Answers 9

38

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

"${my_variable}"=="foo"
Sign up to request clarification or add additional context in comments.

Comments

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:

3 Comments

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)
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?
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.
17

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

A better option (default starting from JMeter 4, see Bug 61675 ) is to check the box Interpret Condition as Variable Expression?, then in the condition field you have 2 options:

  • Option 1: Variable. 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}
    

    Screenshot: If Controller starting with JMeter 3.4

    or any variable you want that contains true or false

    ${myVar}
    
  • Option 2: Function. 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,)}
    

    Screenshot: If Controller with expression starting with JMeter 3.4

Starting with 4.0, if you don't use the checkbox Interpret Condition as Variable Expression?, then this RED warning 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: Master Apache JMeter -- From load testing to DevOps.

Comments

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";

Comments

9

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

Using the quotes was my solution.

Comments

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.

Comments

3

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

Comments

3

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

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

Comments

0

Check the image

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.