开发者

How to dynamically set the HTTP Referer header in JMeter?

Hopefully this is a common issue with JMeter testing.

I setup a Proxy Server via JMeter to 'record' a test case in our DEVELOPMENT environment. That worked fine and I setup some 'HTTP Request Defaults' to globally set the default 'Server Name or IP' and 'Protocol'.

Now that the tests are completed on DEV, we have moved our code to our QA servers for more testing -- and I thought I would simply have to change the 'Server Name' in my defaults and then run the tests.

One thing I noticed is that for each sampler in my test case, other headers are present in the 'HTTP Header Manager' for that specific sampler -- like User-Agent, Cache-Control, etc. The Referer header is the only one that concerns me because it still is 'hard coded' to be the DEV server.

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

Any help is appreciated! Thanks.

jg开发者_StackOverflow社区


Jmeter will read the HTTP Request Defaults only if no value is provided in the HTTP Request. If you leave the domain blank in the request, it will read from the request; else it use the local value.

As a quick fix, open your JMX file in a text editor and replace all instance of the domain with a blank value. Once you open your file back in Jmeter, add the domain value back into your HTTP Request Default config element.

To avoid this in the future, fill out the Request Default values BEFORE recording. Jmeter will automatically filter out values that match the default.

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, any time Jmeter detects '123' while recording, it will replace it with ${customerID}. So, if there is the value '12345', Jmeter will record ${customerID}45


  1. Set JMeter variable named server at root of test plan
  2. Use that variable in HTTP request defaults Server Name field
  3. Use that variable in HTTP Header Manager Referer field setting

Also you may use JMeter property instead of variable if you want to pass it from command line to non-GUI JMeter run


I did this:

  • created User Defined variables named SRVNAME and SRVPORT

  • changed http referrers values in jmx file to be like this:

<elementProp name="Referer" elementType="Header">
  <stringProp name="Header.name">Referer</stringProp>
  <stringProp name="Header.value">http://${SRVNAME}:${SRVPORT}/SomePage.html?querystring=blah</stringProp>
</elementProp>
  • Assigned values to variables with overrides, so that properties could be supplied from the command line:
${__P(srvname,www.defaultserver.org)}
${__P(srvport,80)}
  • Used the variables in HTTP Request Defaults too, in "Server Name or IP" and "Port Number" text boxes.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜