eeadev
eeadev

Reputation: 3852

How to use def variable as condition of an if controller - JMeter

I have such define inside a JSR223 Assertion:

def code = 123

is there a way to use it as condition of my if controller?

I tried with

${code}=='123'

but it does not work.

Here is the Thread Group:

enter image description here

Upvotes: 1

Views: 2352

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

You can check which variables are defined in JMeter using Debug Sampler and View Results Tree listener combination, if the variable is not set - you can use the same approach to see where it got "lost". JSR223 Test Elements errors normally can be found in jmeter.log file.

Coming back to your question:

  1. You need to store your code into JMeterVariables class instance like:

    def code = 123
    vars.putObject('code', 123)
    
  2. Amend your If Controller's condition to look like:

    ${__groovy(vars.getObject('code') == 123,)}
    

More information: How to Debug your Apache JMeter Script

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

You must add code as a JMeter variable:

vars.put("code", code.toString());

And then check with jexl3 or groovy in If Controller:

${__jexl3(${code} == 123)}

Reference from If Controller component

For example, previously one could use the condition: ${__jexl3(${VAR} == 23)}

Also you can see String in Groovy:

def greeting = "Hello ${name}"

assert greeting.toString() == 'Hello Guillaume'

Upvotes: 2

Related Questions