Carlo
Carlo

Reputation: 1714

Reference a variable within a variable in JMeter

I'm working with JMeter. I'd like to specify the test host using user defined variables, like this:

variable name       value  
localhost           localhost  
test                192.168.0.1
hostname            ${localhost}  

Executing the test, I see that the hostname value is not substituted, and obviously the test fails. I know I can use properties and pass the hostname from the command line, or simply change the hostname value. Is it possible to it like I've explained?
Thanks.

Upvotes: 20

Views: 25074

Answers (4)

Shai Alon
Shai Alon

Reputation: 990

Check this forum: from Blazemeter

For example: getting value of varible "listid_XXX" where the XXX number comes from an index variable:

${__V(listid_${idx1})}

Upvotes: 2

Thelesserknowngiant
Thelesserknowngiant

Reputation: 181

To solve this you should use hostname = ${__eval(${localhost})}
http://jmeter.apache.org/usermanual/functions.html#__eval
Carlos' answer has a mistake (which I can't comment on due to rep) as it uses evalVar, this requires as an argument a plain string:

This works: ${__evalVar(localhost)})
This works: ${__eval(${localhost})}
This doesn't work (the current answer): ${__evalVar(${localhost})} http://jmeter.apache.org/usermanual/functions.html#__evalVar

Upvotes: 10

manikanta
manikanta

Reputation: 8500

New newer versions (from 2.2), you can use ${__V(${...})}/.

Ex: ${__V(${SERVER_CONTEXT})}/rest

As @Alies Belik mentioned, if you get

ERRROR jmeter.functions.EvalVarFunction: Variables have not yet been defined

then define the 2nd variable in next other UDV (User Defined Variables) node.

Upvotes: 1

Carlo
Carlo

Reputation: 1714

I've managed to solve my problem. I've changed the hostname variable value to: ${__evalVar(${localhost})}, but I've got this error:

ERRROR jmeter.functions.EvalVarFunction: Variables have not yet been defined

So I've moved the hostname variable declaration in a "User defined variable" child node of my Sampler node. That solved it.

Upvotes: 12

Related Questions