Use JMeter Variable in Groovy Code

10,505

Solution 1

There are several of getting it:

  1. Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as Parameters which contains the full string passed in the "Parameters" section
  2. Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as args[0] (if you pass more than one variable separated by spaces you will be able to refer 2nd variable as args[1], 3rd as args[2], etc.
  3. You can access it as vars.get('user') where vars stands for JMeterVariables class instance
  4. You can access it as vars['user'] - basically the same as point 3 but uses some Groovy Syntax Sugar
  5. You can access it as ctx.getVariables().get('user') - where ctx stands for JMeterContextService class instance just in case (in some test elements vars shorthand is not available)

Demo:

JMeter Groovy Get Variable Value

Solution 2

Any JSR223 element including Assertion have few variables it can use out of the box.

One of the variable is vars which is basically a map of JMeter stored variables.

User Defined Variables row is creating a JMeter variable, so you can get your value Justin in JSR223 using vars.get("user")

Share:
10,505
Justin Holze
Author by

Justin Holze

Updated on June 12, 2022

Comments

  • Justin Holze
    Justin Holze almost 2 years

    I have an Config Element in JMeter, Especially User Defined Variables.

    I have the variable "user" with the value "Justin", How can I use this variable in the groovy code (of an JSR223 Assertion)?