Siraj Sherriff
Siraj Sherriff

Reputation: 21

Jmeter dynamic URL property with variable not substituted

I have a simple Jmeter test where I have a property to set the URL. The PATH in the Jmeter test is set to the following.

${__P(GET_URL,)}

This works well for all URLs that I have been working with, except for the ones where I need to pass a variable in the URL component.

For example, it works for http://server:port/getemployeelist when I run the test with -JGET_URL=/getemployeelist

Then I created a CSV config element to populate the variable EMP_ID.

Then if I run the test with -JGET_URL=/getemployee/${EMP_ID}, the EMP_ID variable is not getting substituted. Jmeter test gives me an error as follows:

java.net.URISyntaxException: Illegal character in path at index xx: https://://getemployee/${EMP_ID}

Appreciate any help/pointers.

Upvotes: 1

Views: 12381

Answers (2)

Siraj Sherriff
Siraj Sherriff

Reputation: 21

Had asked this question a while back. Thought of posting the solution which I eventually ended up implementing. In the solution, I created a template jmx with a substitution variable for the HttpSampler.path and then replace the path at runtime. Following are the key points from the scripting done.

This turned out to be a simpler solution that worked for all kinds of API call patterns.

  1. Created a template jmx (jmeter_test_template) with the following line.

<stringProp name="HTTPSampler.path">#PATH#</stringProp>

This jmx has CSV config element to populate variable "EMP_ID". To create this file, just create a new test with any URL and then save it as a template and replace the URL with substitution variable #PATH#.

  1. Created a wrapper script like run_any_api.sh with usage,

sh run_any_api.sh URL=http://server:port/myapp/employees/${EMP_ID}

  1. In the wrapper script, this URL is replaced in place of the token.

sed "s/#PATH#/$URL" jmeter_test_template.jmx > jmeter_test_template.current_test.jmx

jmeter -t jmeter_test_template.current_test.jmx

  1. Last but not the least, please remember to cleanup the temporary jmx,

rm jmeter_test_template.current_test.jmx

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168132

It will not work this way, JMeter doesn't know anything about ${EMP_ID} at the time it is being started, you need to append this ${EMP_ID} to HTTP Request sampler "Path" in the runtime

  1. Start JMeter as:

    jmeter -JGET_URL=/getemployee/
    
  2. Use CSV Data Set Config to read the EMP_ID from the CSV File

    JMeter CSV EMP ID

  3. In the HTTP Request sampler use construction like /${__P(GET_URL,)}/${EMP_ID} to combine JMeter Property specified via -J command line argument and JMeter Variable originating from the CSV file.

    JMeter properties and variables concatenation

If anything goes wrong first of all check jmeter.log file - it normally contains enough troubleshooting information. If there is nothing suspicious - use Debug Sampler and View Results Tree listener combination to inspect requests and response details, variables and properties names and values, etc.

Upvotes: 0

Related Questions