Unable to correlate value[token], from one sampler to another in JMeter - jmeter

I have recorded script using BlazeMeter plugin and I want to the use access token which I receive in successful login request, in another request. My Test plan looks like as below
Thread Group : [A]
|- HTTP Sampler - Login Page
|-Regular Expression Extractor [getToken]
|-HTTP Sampler - Other Page
|-Beanshell PreProcessor[Set Header in Authorization]
Regular Expression Extractor parameters and values like below :
Variable Name : token
Regular Expression : {“access_token”:”(.+?)"
Template : $1$
Match No. : 0
Beanshell PreProcessor script like below
import org.apache.jmeter.protocol.http.control.Header;
log.info("Start");
sampler.getHeaderManager().add(new Header("Authorization","Bearer"+vars.get("token")));
log.info(vars.get("token"));

Most probably your Regular Expression Extractor fails as your quotation marks look utterly suspicious. You can double check if the token variable really has the anticipated value using Debug Sampler and View Results Tree listener combination. Also check out jmeter.log file for any suspicious entries, if your Beanshell script fails - the cause will be printed there.
The response data of the Login Page seems to be JSON therefore it makes sense to use the JSON Extractor instead of the Regular Expression Extractor. It allows using JSON Path language in order to extract "interesting" bits of data from the responses. In your case the relevant JSON Path expression would be $.access_token
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to the JSR223 PreProcessor and Groovy language (you can re-use the same code)
You don't even need the scripting, you can add Authorization header (as well as any other header) using HTTP Header Manager

Could you add debug sampler and try first to confirm your regular expression extractor working as expected? It should provide you the required value of token.
If your token has the required value, I will suggest you to add HTTP Header Manager config element by right clicking on HTTP sampler
HTTP Request => Add => Config Element => HTTP Header Manager
In this config element, you can visually add the Headers as below:
Please Note That:- You have not provided any space/hyphen(-) or between keyword Bear and token.
Refer this link for details :-
https://stackoverflow.com/a/24550552/1115090

Related

Extracted Regex Value not getting passed into next POST request body in Jmeter

In my first request I am able to extract the value using Regular Expression Extractor which is clearly visible into the debug sampler. The value is extracted by setting the following options in Regular Expression Extractor:-
Name of Created Variable:- instanceUID
Regular Expression:- "InstanceUid":"(.*?)"
Template:-$1$
Match No:-1
Default Value:- (Blank)
The value that I want to pass in the next POST request is visible as:-
instanceUID_g1=2ab5dfb8-a217-4ff2-9025-523565b7b7ad
And the body for the next HTTP POST request is set like this:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"${Registry}"}}
When this request seen in detail inside View Results Tree looks like:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"AAX"}}
As seen the value of ${instanceUID_g1} did not get substituted in the POST body as was for variable ${Registry} which was taken from CSV config.
Being new to Jmeter can anyone suggest what did I miss?
Most probably your Regular Expression Extractor placement is not very correct, be informed about JMeter Scoping Rules concept
If you place Regular Expression Extractor as a child of the request - it will be applied to this request only
If you place Regular Expression Extractor at the same level as several requests - it will be applied to all of them
in the latter case it will be applied 1st to 1st sampler, then to Debug Sampler, then to 3rd sampler so on 2nd step it will be overwritten, most probably that's your problem
Also it appears that you're getting the data from JSON so it makes more sense to use JSON Extractor or JMESPath Extractor

JMeter variable not being used in POST request

I am extracting several variables from a login response, using "Regular Expression Extractor" post processors. Most of them work when I reuse the variable, but one doesn't.
The regex extractor has the following settings:
Apply to: Main sample only
Field to check: Body
Name of variable: id_token
Regular Expression: <input type="hidden" name="id_token" value="([^"]+)"
Template:
Match No.:
Default Value:
Use empty default value: (unchecked)
Then in a later POST HTTP request I use the parameter:
Name, Value, URL Encode?, Content-Type, Include Equals
id_token, ${id_token_g1}, true, text/plain, true
When running the test in the "View Results Tree" listener the same POST request's POST data contains
&id_token=%24%7Bid_token_g1%7D
Instead of the value of id_token_g1.
Other variables in the same POST data are being completed correctly.
The Debug PostProcessor from the previous request contains
id_token_g1=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjVEbHphYTlvNEdveWFtWXdLNl9MUS16akFZTSIsImtpZCI6IjVEbHphYTlvNEdveWFtWXdLNl9MUS16akFZTSJ9.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDM4Ny9pZHMiLCJhdWQiOiJkOGVjZjYzMi1mNzE0LTQ2MzEtOGViOS1lYmUzMDI5OGNjMGYiLCJleHAiOj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CudIgN5D4tgzSmEpQrcK-NasXWYf6RMfeDhYTYAbAChgwJ3KwWk6u_5OsX2LgQGbAg23BH9O_9rGg84DTgHM6IMlQG_loX2PnL8B5yqxydbJsz6CBdiLqKg
I have the same issue with session_state_g1
session_state_g1=XZkcgcg9i_FqlFIS-scHuHqMdm5jepyCSSFkF_S4Dx8.99d68c0a8e802c8767d22fb019bd34b5
But not with
access_token_g1=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjVEbHphYTlvNEdveWFtWXdLNl9MUS16akFZTSIsImtpZCI6IjVEbHphYTlvNEdveWFtWXdLNl9MUS16akFZTSJ9.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDM4Ny9pZHMiLCJhdWQiOiJodHRwczovL2xvY2FsaG9zdDo0NDM4Ny9pZHMvcmVzb3VyY2VzIiwiZXhwIjoxNTI5NTA2MzAzLCJuYmYiOjE1Mjk1MDI3MDMsImNsaWVudF9pZCI6ImQ4ZWNmNjMyLWY3MTQtNDYzMS04ZWI5LWViZTMwMjk4Y2MwZiIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJkeW5hbWljY2xhaW1zIiwiZmlsZXN0cmVhbWluZ2FwaSIsInVzZXJtYW5hZ2VtZW50YXBpIl0sInN1YiI6ImU2NWE0M2MwLTllY2EtNDRjYi05YzYzLWU2NjhhMzNhNWQyYiIsImF1dGhfdGltZSI6MTUyOTUwMjcwMSwiaWRwIjoiaWRzcnYiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhbmdlbGFjIiwibG9jYWxlIjoiZW4tR0IiLCJuYW1lIjoiQW5nZWxhIENhdGFsZG8iLCJhdWRpdF9yZWFkX2JpdG1hc2siOiIwIiwiYXVkaXRfd3JpdGVfYml0bWFzayI6IjAiLCJhbXIiOlsicGFzc3dvcmQiXX0.Z2RTMN6NGwC1e8bJANmzvA9rUyo03vCpA3TTNylbdR8EpOvaktgHXHjWCFzxlaXeiL-24DlmVoq3qPHRx0u03nr8ZRD2fb9R_OeHxZ-gVr-2D1Ash1f_429J_4U7zjQfAQSOKHHamzTocSl0orLuvQdc9-oAydPCzpp82UbEDwsnQl_E52dK_LEB4eSiV3sB8rSpmZkmnhCmuSh9iUuMEOmA_zIs2cH_exFWjv5WXbZ8gKR-ATrPNuzoDpghMNZs-Of-dZwsaHfSJpV8C12DQ6wZq-w7m6v2szUpgJh1kHafKzM8Gm8-nMAAPV83hbdALFVvsF9tfcLjin3OEdytsfsfRLDEHLh4E-vo-LyAeHV-TKGuvX60-6tdQsSQXY-egVx1W7NiObmLbFzdRGAiV--tk8QHJ8Vf-nVAT00YXeQxEdMl1lqJFNuNOFxoNXL1ud_frUv1c2xwcnCH0hBpt2avWqN72Bj-15j6uube7IPIVp20NpT0M7FujohB-wQLfVJ8d7Ac_AVNScqasdijpTP5DzmsFybvXAu8n0MEcYHYF0-C8_d0EWW7GxpSEeeVFqKNk7JFgXEo1ta_5Yu7XDpwG7evzMFMw49cImZgcQlxrq3oDnl-qo6r9UXOfvS5QoDILU6dqYUpRSS1-Mz4_JbOmEs78v5ixcrm94PfNis
I have the same problem if I update the name of the HTTP Request sampler (it shows as ${id_token_g1} in the "View Results Tree" listener)
Any ideas what I'm doing wrong?
Make sure your setup is following JMeter Scoping Rules, i.e. you should have setup like:
Open Login Page
Regular Expression Extractor
Perform Login
Make sure you have this hidden input with name id_token in the response data. JMeter can be configured to store response data in .jtl results file, you need to add next 2 lines in user.properties file:
jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
Don't forget to restore the default configuration once you figure out the cause of the problem. See Results file configuration for more details.
Regular expressions is not the best choice for parsing HTML responses as they can be very fragile in case of markup change (changed order of HTML attributes or the input definition becomes multi-line). So consider switching to CSS/JQuery Extractor instead, the relevant configuration would be something like:
Reference Name: anything meaningful, you can keep id_token
CSS/JQuery Expression: input[id=name]
Attribute: value
In 2nd POST request you can refer the extracted value as ${id_token}
1.Follow Jmeter Scoping Rules.
2.To your regular expression you can give template as $1$ and use ${id_token} instead of ${id_token_g1}.
For more information on regular expression extractors
follow this link.
You can use this website to test your regular expressions.
Let me know if it helps..

JMeter reponse headers values before redirection on status code 302

I am performing load testing on an API using JMeter. For that, I call an oauth link, which returns a code in the headers which I use for further testing. But the link redirects to another link and I am unable to capture the value of the response headers when a response with status code 302 is returned. How can I do that.
If your situation is like this one:
You can still extract the dynamic value from the latter sample result by modifying Regular Expression Extractor scope
As per documentation:
Apply to:
This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
Main sample only - only applies to the main sample
Sub-samples only - only applies to the sub-samples
Main sample and sub-samples - applies to both.
JMeter Variable - assertion is to be applied to the contents of the named variable
By default Regular Expression Extractor is looking into Main sample only, in the above example it is HTTP Request, if the data you're looking for is stored in one of the sub-samples it is enough to change Regular Expression Extractor's scope to look into sub-results as well:
You have 2 options:
Do not allow HTTP request to redirect. Simply uncheck "Follow Redirects" checkbox in the HTTP Sampler:
That way you can process this request normally. The drawback is of course that you need to add a second request which will take you to a link to which you are normally redirected automatically.
Most post-processors allow you to extract value from either main sample, or sub-samples, or both. So follow redirect as before, but change Post-Processor to extract value from sub-sample. For example Regular Expression Extractor:

Getting oauth token using Jmeter xpath extractor and using it in path of other Http Request

What should be the Regular expression if I want to extract the value for access token, So that i can use it in Path of other Http request ie like access_token=93ee29b4-74dc-4uu7-8e10-6eac6845511b from below http response.
{
"access_token":"93ee2tum-1234-56789-8e10-6eac684551tum",
"token_type":"Bearer",
"expires_in":3600,
"scope":"test"
}
I have given regular expression as
"access_token":"([^"]+)"
And also where can i check the value of regular expression I am getting
You can test your Regular Expressions using View Results Tree listener in RegExp Tester mode like:
Also you can use Debug Sampler to see the associated JMeter Variables. See How to Debug your Apache JMeter Script article for details.
Starting from JMeter 3.0 it's more handy to use JSON Path PostProcessor, in that case the relevant JSON Path Expression would be as simple as:
$.access_token
You can add following regex. It will get the token and depending on the name variable you set, you can add one BeanShell Post-processor to print the token in console for you to verify. Just add the following to it:
log.info(vars.get("token"));
// token is the variable name which you specify in Regular Expression Extractor.

How to Re-use data generated by one Response to other request?

In my application while executing the first request one unique key is generated which key is required for Next all the request. let me how to automate such scenario in Jmeter.
The process should look as follows:
Add Post Processor to the first request
Configure it to extract the required value and store it into a JMeter Variable
Use JMeter Variable from step 2 in your next request.
Depending on response data type you have the following choices:
Regular Expression Extractor - for text
CSS/JQuery Extractor - for HTML
XPath Extractor - for XML and XHTML
JSON Path Extractor - for JSON
It is also possible to extract data from files i.e. if response is in PDF format, but it's a little bit tricky
Example configuration to store the whole response:
Reference Name: any suitable variable name, i.e. response
Regular Expression: (?s)(^.*)
Template: $1$
You can refer the extracted value as ${response} where required. You can also amend the regular expression to extract response part instead of the whole response. JMeter uses Perl5-compatible regular expressions, see Regular Expressions User Manual Chapter for details
You can use regular expression extractor to extract the key from the response of your first request and use the extracted key for subsequent requests. To achieve this:
Right click on the first request and add post processor: Regular Expression Extractor.
Create your regular expression and provide values in other required fields. Please refer to JMeter component reference http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor
Extracted value will be saved in the variable given as reference name
Use this variable in subsequent requests
Here is an example test plan with results.

Resources