Use of Json extractor - jmeter

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)

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

How to pass the dynamic value in Body data in the Post Request using jmeter

I recorded the .JMX script in Jmeter and one of the request is as below
POST http://www.hello.com/auth/nqa/md/login
Body data:
{"domainId":"nqa","code":"12345skdkdk"}
I would like to send the "code" field dynamically and for that I added the regular expressing extractor as below enter image description here
When i re run the script , the code value is not replaced with the dynamic value.
Not sure what part i am missing in the regular expression extractor or in the Body data field
First of all, you cannot extract the value from the request body using the Regular Expression Extractor, normally you should extract the dynamic values from the previous response so inspect the whole flow using View Results Tree listener and look for your "code" value there
Your regular expression extractor in its current configuration will return random value in the parentheses so it could be domainId, nqa, code or 12345skdkdk. Going forward if you need to get some dynamic data from JSON go for JSON Extractor or JSON JMESPath Extractor
List item
you should do three-step of below
go to the random variable according below picture
Define random variable name . in this case we set variable name to code1 and set min & max value to this
Use ${code1} variable to your data section

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

How to loop an HTTP request and update the variables each time in Jmeter BeanShell

I have 2 HTTP request: One to GET data from an api and the other to POST data to the api.
The GET request brings several users in JSON. The POST request requires to 1 request per user. Hence I need to:
Loop the same POST request several times depending on the number of users (already did this by using a while controller that checks the number of users from the JSON response).
For each POST request, I need the variables used in such request to be updated depending on the user's information inside the JSON response.
The approach I am attempting is to use BeanShell PreProcessor inside the POST request but I'm having troubles with it.
Suppose one variable is called ${name} in the request body of the POST. I am using a JSON Extractor PostProcessor (On the GET request) path: "Travelers[0].FirstName" that returns the first name of the first user but then I need the second user's name (Travelers1.FirstName) to be assigned to the same variable ${name} before the POST request is sent and so on with every single user.
I want to make a for loop like this:
for (int i = 0; i <= numberOfTravelers; i++) {
vars.put("Name", Travelers[i].FirstName)
}
The problem is that I do not know how to call a JSON path from a JSON response of another previous request. Is there a way to reference the jsonpath to the specific JSON response or a way to save the whole JSON response in a variable and then find the specific value inside that variable as a JSON path.
I have tried this with the JSON extractor but the problem is that if I use the path: Travelers[*].FirstName, it will actually get all the names on the JSON but the variable ${name} will only store ONE name, not all of them as an array that I can later access in my for loop with a normal variable ${name[i]}. That is why I need to access the JSON path from the BeanSheall.
Here a sample of the JSON response:
{
"Travelers":
[
{
"FirstName":"VICTOR",
"Surname":"ORREGO",
"Key":"1.1",
"PassengerRPH":1,
"TypeCode":"ADT"
},
{
"FirstName":"JULIO",
"Surname":"OZAETA",
"Key":"2.2",
"PassengerRPH":2,
"TypeCode":"ADT"
}
]
}
This is the PostProcesor JSON Extractor at the GET request that I'm using. it is currently assigning the first name it gets from the JSON response (Victor) to the variable ${Name}
I need that in the next iteration (of the POST Request) the variable ${Name} to return the next name in that path, which is Julio.
here is the solution..
Add a JSON Extractor to the get request .. use match no -1 to store all Firstnames as show below.
i'm extracting all Firstnames and storing it in JMeter variables with a single JSON extractor
2. To the same get request add a JSR223 Post processor and set the counter value to 1
vars.put("counter","1");
Add a while loop to the test plan and add the following condition to the while loop.
${__javaScript(parseInt(${counter})<=parseInt(vars.get("FirstName_matchNr")),)}
4.To the post request add a JSR223 Pre Processor and add the following code
vars.put("name",vars.get("FirstName_"+vars.get("counter")));
This will store FirstName_Matchno's value in name variable.
Add a JSR223 Post Processor to the POST Request and increment the counter.
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its substituting a different name on each iteration of loop
Let me know if it helps..

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