joCha
joCha

Reputation: 380

JMeter If Controller using groovy and Or is not working

I have a script where I have some if controllers. I'm attempting to add a 4th If controller that will trigger a script failure if none of the 3 expected values is returned. It's saying one of the 3 expected values is invalid.

1st, I have a user defined variable like this:

testTool= ${__P(testTool,APPLES)}

2nd, I have these 3 If controllers with these Expressions:

  1. ${__groovy(vars.get("testTool").toUpperCase().equals("APPLES"))}
  2. ${__groovy(vars.get("testTool").toUpperCase().equals("BANANAS"))}
  3. ${__groovy(vars.get("testTool").toUpperCase().equals("PEACHES"))}

The 4th If is supposed to be triggered if the value of testTool is not one of the 3 expected values. It's Expression looks like this:

>    ${__groovy( (vars.get("testTool").toUpperCase().equals("APPLES") == false || 
>    vars.get("testTool").toUpperCase().equals("BANANAS") == false ||
>    vars.get("testTool").toUpperCase().equals("PEACHES") == false)) }

I have also tried it this way:

>  ${__groovy((!vars.get("testTool").toUpperCase().equals("APPLES") || 
>  !vars.get("testTool").toUpperCase().equals("BANANAS") || 
>  !vars.get("testTool").toUpperCase().equals("PEACHES")),)}

It is somehow saying APPLES is an invalid testTool. What am I doing wrong? All if controllers have the 'Interpret Condition as Variable Expression' checked.

Upvotes: 0

Views: 4968

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

You should use && operator instead of ||, see Groovy Logical Operators for detailed explanation and more information.

Your 4th expression needs to be amended to look like:

${__groovy(!(vars.get("testTool").toUpperCase().equals("APPLES")) && !(vars.get("testTool").toUpperCase().equals("BANANAS")) && !(vars.get("testTool").toUpperCase().equals("PEACHES")))}

An easier option would be using Switch Controller, from implementation and especially performance perspectives it is the optimal solution.

  • Add Switch Controller to your Test Plan
  • Use ${testTool} as the "Switch Value"
  • Put 4 requests as the children of the Switch Controller and name them as:

    • APPLES
    • BANANAS
    • PEACHES
    • DEFAULT

    So if ${testTool} variable value will be APPLES - the APPLES sampler will be executed, if ${testTool} variable value will be BANANAS - the BANANAS sampler will be executed, etc.

    If ${testTool} will not match any other children - JMeter will run DEFAULT sampler

    JMeter Switch Controller

See Selection Statements in JMeter Made Easy guide for details.

Upvotes: 0

Rohit
Rohit

Reputation: 611

Use the following condition in if controller

 ${__groovy(!(vars.get("testTool").toUpperCase().equals("APPLES"))||!(vars.get("testTool").toUpperCase().equals("BANANAS"))||!(vars.get("testTool").toUpperCase().equals("PEACHES")))}

Please let me know if it helps

Upvotes: 1

Related Questions