Karl
Karl

Reputation: 619

JMeter Variable output as part of property name?

So I am using JMeter 3 for some performance testing and I have just been asked to make it more dynamic...

I am using the Properties File Reader plugin, and pointing it to a file that contains entries like this -

DEV_SEARCH_API_URL=example.com
QA_SEARCH_API_URL=example2.com

Now I have a User defined variable called

env | ${__P(perf.environment, qa)}

I am then performing a HTTP request building a url like this

https://${login_serverName}/${env}/authentication/login

I am calling QA_SEARCH_API_URL like so from another UDV

login_serverName |  ${__P(QA_AUTHENTICATION_API_URL)}

And this works, but what I want to be able to do is replace the QA part of the variable with the value coming from the UDV ${env} (as this will be QA or DEV) thus making the correct call for the url from my properties file...

I have tried

${__P(${env}_AUTHENTICATION_API_URL)}

But this doesn't work and doesn't return anything, I don't believe that this isn't possible, and I am just doing something wrong...

Any help would be most appreciated, thank you.

Upvotes: 1

Views: 1459

Answers (3)

Vardogr
Vardogr

Reputation: 11

${__groovy(props.get(vars.get("env") + "_AUTHENTICATION_API_URL"))}

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168217

It is possible, just use __V() function. As per documentation:

For example, if one has variables A1,A2 and N=1:

  • ${A1} - works OK

  • ${A${N}} - does not work (nested variable reference)

  • ${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1

  1. If env is a JMeter Variable:

    ${__V(${env}_AUTHENTICATION_API_URL)}
    
  2. If env is a JMeter Property:

    ${__V(${__P(env,)}_AUTHENTICATION_API_URL)}
    

More information and hints: Here’s What to Do to Combine Multiple JMeter Variables

Upvotes: 3

RowlandB
RowlandB

Reputation: 573

TL;DR

When you're putting variables inside each other, you almost always want evalVar.


There are a couple ways to talk about variables (and properties) in JMeter.

Example Variables:

var | something <= the variable we're going to be putting into other stuff

something_more_stuff | ${something_else} <= the variable that will be made by combining things

something_else | even more stuff <= the variable that we're trying to get


Example Queries:

${${var}_more_stuff} | ${${var}_more_stuff}

${__V(${var}_more_stuff})} | ${something_else}

${__eval(${var}_more_stuff})} | var_more_stuff

${__evalVar(${var}_more_stuff})} | even more stuff


As you can see, putting "variable brackets" inside each other just doesn't work. Everything else does something slightly different, which is interesting, but not necessarily useful. When you're putting variables inside each other, you almost always want evalVar.

Upvotes: 4

Related Questions