How to dynamically set HTTP Referer header in JMeter?

Hope this is a common JMeter testing issue.

I install a proxy server through JMeter to "record" a test case in our DEVELOPMENT environment. This worked fine, and I configured a few “ HTTP requests by default ” to globally set “Server Name or IP Address” and “Protocol” by default.

Now that the tests are completed on DEV, we moved our code to our QA servers for more testing - and I thought that I just needed to change the “Server Name” in my default settings and then run the tests.

One thing that I noticed is that for each sampler in my test case, other headers are present in the " HTTP Header Manager " for this particular sampler - for example, User-Agent, Cache-Control, etc. The Referer header is the only thing that concerns me, because it is still “hard-coded” as a DEV server.

Is there a way to set this dynamically - so that I can run this recorded test in multiple environments? How is this usually handled?

Any help is appreciated! Thanks.

Jg

+4
source share
3 answers

Jmeter will read the default HTTP request values ​​only if no value is specified in the HTTP request. If you leave an empty domain in the request, it will read from the request; otherwise it uses a local value.

As a quick fix, open your JMX file in a text editor and replace the entire domain instance with a blank value. After you open your file again in Jmeter, add the domain value back to your default HTTP request configuration item.

To avoid this in the future, fill in the default query values ​​before recording. Jmeter automatically filters out the default values.

You can also do this with variables and user defined values . Be careful what default values ​​you set. For example, if you said customerID = 123 , at any time when the Jmeter detects "123" during recording, it will replace it with ${customerID} . So, if there is a value of "12345", Jmeter will write ${customerID}45

+1
source
  • Set a JMeter variable named server in the root plan of the plan
  • Use this variable in the default HTTP request field.
  • Use this variable in the header field of the HTTP header.

You can also use the JMeter property instead of a variable if you want to pass it from the command line to a non-GUI JMeter run

+3
source

I have done this:

  • user defined variables named SRVNAME and SRVPORT created

  • changed the values ​​of the HTTP referrers in the jmx file as follows:

 <elementProp name="Referer" elementType="Header"> <stringProp name="Header.name">Referer</stringProp> <stringProp name="Header.value">http://${SRVNAME}:${SRVPORT}/SomePage.html?querystring=blah</stringProp> </elementProp> 
  • Assign values ​​to variables with overrides so that properties can be provided from the command line:
 ${__P(srvname,www.defaultserver.org)} ${__P(srvport,80)} 
  • The variables used in the default HTTP requests are also indicated in the "Server Name or IP" and "Port Number" text boxes.

Hope this helps

0
source

All Articles