Accessing JMeter HTTP Request Defaults values - jmeter

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

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)

Reading Environment Variable values in Jmeter

I want to parametrize the testdata path, result path, server and port by defining them in environment variable.
I was able to achieve this to certain extent with System.getenv("Jmeter_Result") using it in BeanShell processor. But I need to use configuration element to fetch the value.
Can you kindly suggest.I am using Jmeter 3.1.
According to https://jmeter.apache.org/usermanual/functions.html#what_can_do, ${__BeanShell( ... )} may also help you on this issue.
For instance, one of our APIs needs OAuth2 authentication, and what I did was:
- Thread Group
- User Defined Variables
client_id = ${__BeanShell( System.getenv("client_id") )}
client_secret = ${__BeanShell( System.getenv("client_secret") )}
- Once Only Controller
- OAuth2 HTTP Request
- API HTTP Request
There is __env() function available via JMeter Plugins, it seems to be something you're looking for.
You can install __env() and other custom JMeter Functions using JMeter Plugins Manager.
If the environment variable is made available via a System property then
JMeter can access it using the ${__P("Jmeter_Result")} function.
Otherwise, it's trivial to pass in the value of an environment variable using
the -J option:
jmeter -JJmeter_Result=$Jmeter_Result

How do I determine which HostHeader the client used?

I'm attempting to set some session variables depending on which hostHeader was used to access my site. Do I have to parse it out of the URI property or is there a better way? (MVC3/IIS7)
Check out the HttpWebRequest.Host property, which should be equivalent to accessing Request.Headers["Host"] in MVC.
It mentions it's used to "Get or set the Host header value to use in an HTTP request independent from the request URI." If you're not setting this somehow then "then the Host header value to use in an HTTP request is based on the request URI." So inspecting it via the Request.Url.Host or equivalent property is equally sufficient. I don't know of any other way.

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
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

Resources