Nikhil
Nikhil

Reputation: 393

How to get value of variable defined in testplan and from csv file with groovy in jmeter

I have variable name message defined in test plan. I want it to be a value of data in groovy. After implementing this I am getting error.

I am trying to pass a variable in groovy in JSR223 sampler. The variable is defined in test plan. Basically there are ${user_id} and ${__time(,curTime)} defined for value of Message in Test Plan ${user_id} is the value i am getting from csv file and ${__time(,curTime)} is function for current time in millisecond. Basically i want a value in output as user_id corresponding with timestamp in millisecond.

For E.g if value of ${user_id} is abcd and value of ${__time(,curTime)} is 1478965236574 then, i am expecting value as abcd1478965236574 in the variable data in JSR223 sampler. Can i get this value?

Upvotes: 0

Views: 1088

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

  1. If you want to concatenate 2 variables: user_id and curTime

    def value = vars.get("user_id") + vars.get("curTime");
    
  2. If you want to generate the new timestamp"

    def value = vars.get("user_id") + System.currentTimeMillis()
    

vars is a shorthand to JMeterVariables class instance which provides read/write access to JMeter Variables in scope.

Remember that you should not inline JMeter Variables and Functions like ${Message} into the script body, use vars.get("Message") instead as inlining variables causes compilation caching failure hence you loose the major Groovy benefit. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! for more detailed explanation and scripting best practices.

Upvotes: 3

Related Questions