Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JMeter Variable in Groovy Code

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

like image 496
Justin Holze Avatar asked Nov 08 '17 06:11

Justin Holze


1 Answers

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

like image 132
Dmitri T Avatar answered Sep 30 '22 14:09

Dmitri T