Trying to figure out how I can access elements of a post request body (JSON) and store it as a variable. One of my tests creates a user using ${__UUID}#gmail.com - and I'd like to then check that my response includes this same information.
I'm guessing I could probably create the UUID before the request and store it as a variable, and then check against that, but wondering if there is anything similar to JSON Path Extractor for request elements.
There is a JSR223 PreProcessor you can use to fulfil your requirement.
Assuming you have JSON Payload like:
{
"user": "${__UUID}#gmail.com"
}
Add JSR223 PostProcessor and put the following code into "Script" area:
def user = com.jayway.jsonpath.JsonPath.read(sampler.getArguments().getArgument(0).getValue(), '$..user').get(0).toString()
log.info('Random user email:' + user)
vars.put('user', user)
The above code will:
Extract from the request everything which matches $..user JSON Path expression
Print it to jmeter.log file
Store the value into a JMeter Variable so you will be able to refer it as ${user} where required.
More information:
Apache Groovy - Why and How You Should Use It
Groovy - Parsing and Producing JSON
Related
I am trying to obtain data from sent JSON and use it further in another request.
My sent JSON also has dynamic variables like ${data} so the trick is that it has to execute first in order to be able to extract.
Let's say I have the following SENT JSON:
{
"field_one": ${data1},
"field_three": [more data],
"field_two": ${data2}
}
Question is: How can I extract "field_one" and "field_two" values from the sent request?
Thanks
You don't need to extract them, they are ${data1} and ${data2} so you can re-use these JMeter Variables anywhere in the script.
If I don't understand something obvious or you need to copy the values to another JMeter Variables, you can extract them as follows:
Add JSR223 PostProcessor as a child of the request which sends above JSON
Put the following code into "Script" area:
def requestBody = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put('field_one', requestBody.field_one)
vars.put('field_two', requestBody.field_two)
That's it, now you should have ${field_one} and ${field_two} JMeter Variables holding the values you're looking for.
In the above example sampler stands for HTTPSamplerProxy and vars for JMeterVariables, check out Top 8 JMeter Java Classes You Should Be Using with Groovy for details on the above and other JMeter API shorthands available for JSR223 Test Elements.
More informaion:
JsonSlurper
Apache Groovy: Parsing and producing JSON
The response is in JSON format but lines are commented.
I want to use retrieve the key-value pair from the response dynamically and use in Jmeter
I am beginner to JMeter.
Add JSR223 PostProcessor as a child of the request which returns the "commented" JSON
Put the following code in "Script" area:
def before = prev.getResponseDataAsString()
log.info('Before:' + before)
def after = before.replaceAll('/\\*','').replaceAll('\\*/','')
log.info('After: ' + after)
prev.setResponseData(after.getBytes('UTF-8'))
Tick 'Cache compiled script if available' box
Run your test
Now you should have the data without comments, it will be suitable for using JSON Extractor
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It
I will get json response from last request, then I will parse the response and get a variable array, then create new request base on each element in that array one by one. I don't know how to implement it.
Use JSON Extractor and ForEach Controller combination. The idea is to have variables like:
var_1=foo
var_2=bar
var_3=baz
So you would be able to iterate them using foreach loop. See Using Regular Expressions in JMeter article to get the overall idea.
Steps to follow:
Add JSON Extractor (>= 3.0 version) Or JSON Path Extractor (< 3.0 version) plugin, to the HTTP Request sampler, as a child, in which JSON response is received.
Add the JSON Path Expressions to capture the specific values and store it in variable names, say capturedArray. refer JSON Path Syntax.
In later requests, i.e., HTTP Request Samplers, you can retrieve the array value by using the syntax ${capturedArray}
suppose I have a webservice that generate random password and return json response, this password is used in the following request. is there anyway to do this in jmeter, i.e to extract the password from the first response and use it in the next request
We have Regualr Expression Extractor Or JSON Path Extractor in JMeter to extract the values from 1 response & use it in subsequent requests.
As Vinoth S said, JSON extractor is the way to go. You can download it from here. Document how to use it is here. Just put it in request from which you want to extract, type vairable_name that you will use in the next request as ${variable_name}, JSON Path is quite easy to set, probably, if the response is not to complicateg just the property name from JSON will be enough, or something like $.varName and you're ready to go.
I have some specific scenario for retrieving Access Token from API using jmeter.
I need to implement following test case and have no idea how to implement this (I am new in jmeter):
Send GET request to server.
Server will return following response: {"RequestToken":"81d1fcd6146d41f69a966a2c083382c7","Expires":3600}
After that I need to send POST request to server with parameter of "RequestToken" from step #2.
Thanks!
Answer of Dmitri T really helped me! Thanks a lot!
If your response {"RequestToken":"81d1fcd6146d41f69a966a2c083382c7","Expires":3600} is the full one you can add a Regular Expression Extractor Post Processor to GET request configured as follows:
Reference Name: anything meaningful, i.e. token
Regular Expression: {"RequestToken":"(.+?)","Expires":3600}
Template: $1$
After that you can refer to extracted value as ${token} or ${__V(token)} in POST request.
If you need to deal with more complex JSON structures I would recommend using JSON Path Extractor available via JMeter Plugin. It allows fetching data from JSON responses in more "intelligent" way as big JSON entities cannot be easily parsed via regular expressions.
In this case relevant JSON Path query will look like $.RequestToken.
See Using the XPath Extractor in JMeter guide for more details (scroll down to Parsing JSON).
Hope this helps.