How to dynamically set the HTTP Referer header in JMeter? - windows-7

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

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

Set JMeter variable named server at root of test plan
Use that variable in HTTP request defaults Server Name field
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

Related

Accessing user defined variables in JMeter "HTTP Request Defaults"

I have a number of user defined variables configured in JMeter. I have an "HTTP Request Defaults" ConfigElement, and I want to use parameters for "Protocol", "Server Name or IP", and "Port Number".
I tried using ${__P(HTTP_SCHEME, https)} as an example and it did not work properly - it always uses the "default" value provided.
Is this even possible?
If you have a User Defined Variables configured like:
you can refer the defined variable as ${HTTP_SCHEME} where required, including HTTP Request Defaults:
If you use ${__P(HTTP_SCHEME, https)} it reads the value from JMeter Property, the property can be defined either in user.properties
HTTP_SCHEME=file
file or passed via -J command line argument
jmeter -JHTTP_SCHEME=http
More information: Apache JMeter Properties Customization Guide

Jmeter cookie handling while page redirect

Scenario:
Login (URL 1)
Launch link from landing page
Newly launched link (URL 2)
URL 1 and URL 2 has same domains but different sub-domains.
Have added HTTP Cookie Manager
When I am running my script using Jmeter 3.2 which has only HC4CCookieHandler
Login is passing
Launching the link which has different subdomain is failing, and on verifying there are No Cookies added in the request, here the session ID and other cookies should have been added to the request.
Can some one please help here?
Also added Beanshell post-processor to extract cookies from logon response and added as pre-processor to the redirected request, but is giving error.
Error: jmeter.util.BeanShellInterpreter: Error invoking bsh method:
eval Sourced file: inline evaluation of: ``import
org.apache.jmeter.protocol.http.control.CookieManager;
The fact you don't see all the cookies might indicate problems with the cookies. You can enable debug logging of the HTTP Cookie Manager in order to see what's wrong.
Click HTTP Cookie Manager
Click "Help -> Enable Debug" in JMeter's main menu
Observe CookieManager-related messages in jmeter.log file
By default Cookie Manager doesn't store "invalid" cookies (domains mismatch, expired already, etc.), if you are absolutely sure your application acts as it should - you need to "tell" JMeter to store this cookie in any case, you can do it by either adding the next line to user.properties file (lives in JMeter's "bin" folder)
CookieManager.check.cookies=false
JMeter restart will be required to pick the property up
Alternatively you can pass the property via -J command-line argument like:
jmeter -JCookieManager.check.cookies=false -n -t ...
In this case restart will not be required.
In case of manual cookie management in particular and when it comes to using JMeter in the most efficient way in general it is better to avoid scripting where possible. For example you can extract cookie(s) you need from Set-Cookie response header(s)
using Regular Expression Extractor. In case(s) when you have to go for scripting make sure you use the most performing language (which is Groovy)

Accessing JMeter HTTP Request Defaults values

I'm using HTTP Request Defaults to set host and port for a set of requests.
I'd like to access the same host value for cookie domain. Is there a way to refer to that particular variable?
Something like this maybe?
${DEFAULT_HOST}
No. There are no such default variables to access the HTTP Request Default Values.
But - you can easily achieve this using User Defined Variables.
Just create a variable DEFAULT_HOST=www.google.com
Then update the server name of the HTTP Request Defaults to ${DEFAULT_HOST}
You can also access the hostname everywhere in your test by using ${DEFAULT_HOST}.
And for extra credit...
Add a User Defined Variables element to the beginning of the test, so you can pass arguments from the commandline. In my tests, i have
name=HOST
value=${__P(host,test.mysite.com)}
Then you can use ${HOST} for HTTP and cookie defaults (and anything else), and run your test from the commandline like
jmeter -n -t mysite_loadTest.jmx -Jhost=www.mysite.com

How to fetch session id using reqular expression

Hi I want to fetch a session id from following response using reqular expression
<form id="fm1" action="/cas/login;jsessionid=08D3BEEBAAB30126106472355249AB49" method="post">
I want output as jsessionid=08D3BEEBAAB30126106472355249AB49
I have tried many way, but not working
Thanks.
You can do it via Regular Expression Extractor configured as follows:
Field to check: Body
Reference Name: anything meaningful, i.e. sessionId
Regular Expression: jsessionid=(\d\w+)
Template: $1$
try going through jmeter wiki, the latest one covers in a very good and generic way
http://jmeter.apache.org/usermanual/regular_expressions.html
Use following regular expression: jsessionid=(.+?)"
By the way, you can test regex against real response using RegExp Tester option of the View Results Tree listener.
By the way, there is an easier option to capture JSESSIONID value. In majority of cases it is a HTTP Cookie hence you can easily access it's value using HTTP Cookie Manager
Add the following line to user.properties file (it's located under /bin folder of your JMeter installation)
CookieManager.save.cookies=true
Restart JMeter
Add HTTP Cookie Manager to your Test Plan
Access captured cookie value as ${COOKIE_JSESSIONID} where required.
See Using the HTTP Cookie Manager in JMeter article for more detailed explanation of the importance of this configuration element.

Jmeter - Turn off "Keep Alive" option for all http sampler from one place

I am new to Jmeter. I have the .jmx file with around 50 http sampler. Each sampler has "Use KeepAlive" option turned on.
Question is how to turn off this option for each of 50 http samplers from one place.
For particular this situation you can open your .jmx file with your favorite text editor, locate the following line:
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
and change all occurrences to
<boolProp name="HTTPSampler.use_keepalive">false</boolProp>
For other options you can use HTTP Request Defaults feature which will automatically populate blank fields from defaults. Any HTTP Request local changes will override defaults.
To update your existing script you can follow the other Answer.
To prevent your HTTP sampler to have that option when you record, just disable the 'Use Keep Alive' in the recorder. So that you will not have that in the first place!!

Resources