Extracted Regex Value not getting passed into next POST request body in Jmeter - 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

Related

In JMeter when I try to pass the variable assigned with JSON extractor to the subsequent request null value is passed

I do a POST request using Jmeter and I parse the JSON response using the JSON extractor. When i use the debug sampler I could see the Variable is assigned with the value properly but that value goes as null in the subsequent request
Below is the request URL for the next sample /XXX/XXX/XXX/XX/${recordingjobid}
but this value recordingjobid is not substituted with the value.
I can only think of not proper placement of the JSON Extractor.
For example if you have it at the same level as several samplers - it will be run after every of them (including the Debug Sampler) and each next one will be overwriting the value set by the previous one as JMeter Variables are local to the thread and don't allow duplicates.
So my expectation is that if you move the JSON Extractor to be a child of the request which returns the JSON - it should resolve your issue.
More information: JMeter Scoping Rules - The Ultimate Guide

Use of Json extractor

Im running a test plan containing 10 requests i need to get the variable value from 3rd response json to be passed to request URL of 10 th request passed . any way we can use json extractor to do this?
This can be accomplished by using any of the post processors for correlation in amongst any of the requests. I have shown an example below with JSON Extractor
Request Sample
The value that I am going to extract from 3rd request's response is "I am the value to be fetched from 3rd response"
JSON Extractor
I am using the below syntax in JSON Path Expression to extract data from "valueToBeFetched" json object and storing it in variable named "extractedValue_C"
$..valueToBeFetched
10th Request
Replacing the extracted value with syntax ${extractedValue_C} in 10th request
Output
Response Captured from 3rd Response
Captured value passed in 10th request
Hope this helps!
JSON Extractor obeys JMeter Scoping Rules so if you put it as a child of the 3rd request - it will be applied to 3rd request only.
If you come up with a valid JSONPath matching the value you want to extract - the value will be saved into a JMeter Variable which can be used anywhere after 3rd request (where it is defined or overwritten in next iteration)

Unable to correlate value[token], from one sampler to another in 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

jmeter: extract information from headers and save it to a variable

My task is to make a request and save some data from the headers in a variable and use it later in another requests.
So I've added an http request and added an regular expression extractor. Here is my setup:
And then I've created another request and I'm using the LocationToken variable, but there the variable has the default value (dd). here is the response:
So, as you can see, the pattern patches on the headers. So what have I done wrong?
In Regular Expression Extractor, under Apply to section, you should select Main sampler and sub-samplers option but not JMeter Variable.
Regular Expression Extractor saves the value into the variable which we specified in Reference Name field.
in the question,
Reference Name: LocationToken
in later request, you refer the value as follows:
${LocationToken}

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