Storing the output json parameters as variable in jmeter using it in next request - jmeter

I have a http request which gives json output as :
{
"MESSAGE_CODE":200,
"MESSAGE_DESCRIPTION":"OTP Generated Successfully",
"data":
{
"otp":"123456",
"otpGeneratedDate":"yyyy-mm-dd"
}
}
I want to use otp as the input parameter in json for my next http request.
I have added JSON extractor with following configuration :
enter image description here
Names of created variable : OTP
JSON path expressions : $..data.otp
Match No. : 1
But still when I am calling this parameter as
"otpNumber": "${OTP}" in my next input JSON http request, its not getting called.
and value is passed as ${OTP} for otpNumber
How can I handle this

As per JMeter Documentation:
Variables, functions (and properties) are all case-sensitive
So you need to change this line:
"otpNumber": "${OTP}"
to this one:
"otpNumber": "${otp}"
and your test should start working as expected.
You can observe which JMeter Variables are defined along with their values using Debug Sampler and View Results Tree listener combination.

Related

Saving matched values in JMeter postprocessors

In just starting to use JMeter I am trying to set variables of the form taskId_1, taskId_2, taskId_3 (defined in "User Defined Variables") and use them in HTTP Samples (REST requests). When I run postprocessors none of my JSON Extractors or Regular Expression Extractors save the values matched (and I tested the extracted regular expression using RegExp tester.)
The response sent from the GET request that I am parsing looks like (edited for readability):
{
"case-data": {
"Description": "100 parallel tasks",
"Workflow": {
"com.MyCompany": {
"workflow-case-id": null,
"stages": [
{
"stage-guid": "470D2E00-A9E1-11EB-887B-4226FC2CA371",
"tasks": [
{
"task-name": "Stage1Task1P",
"user-group-id": "Joe",
"task-id" : 52
},
{
"task-name": "Stage1Task2P",
"user-group-id": "Joe",
"task-id" : 73
},
{
"task-name": "Stage1Task3P",
"user-group-id": "Joe",
"task-id" : 123
}
]
} ] }}}}
Following the BlazeMeter tutorials, for the Regular Expression Extractor I use
Apply To "Main sample and sub-samples"
Field to check "Response Message"
Name of created variable taskId
Regular expression task-id"\ :\ (.\d+)$
$1$
(I have also tried taskId_1 - my ideal solution would set all the taskId's at once).
For the JSON Extractor that I just tried I use:
Names of created variables: taskId
JSON Path Expressions: $."task-id" (LOG ALL indicates Could not find JSON Path - so I will revise)
Match No: -1
Default Values: taskId not matched
It is as if none of these strings are ever matched so the values are not set - not even default values. I also have a Debug Sample, Debug Postprocessor and a View Results Tree included in the parent Logic Controller - but see no values of variables set anywhere (even in the logs).
I have other JSON extractors that attempt to traverse down the object tree from the top but they also are not setting my User Variables although LOG ALL indicates they are setting the variables of the same name.
What is wrong with my match expressions and assignment parameters?
How can I change my usage of Debug Sampler/Postprocessor/View Result Tree/Properties and variable viewer (which has JMeter variables = True) to observe variables?
Is there a way to run a test plan one step at a time (e.g., a preprocessor, a request, a postprocessor then another postprocessor) by clicking on UI elements?
Regular Expression Extractor:
"task-id"\s*:\s*(\d+)
More information: JMeter Regular Expressions
JSON Extractor:
$..task-id
More information: Jayway JsonPath
To view generated variables just add a Debug Sampler somewhere at the bottom of your script and add View Results Tree listener so Debug Sampler would be in its scope, see How to Debug your Apache JMeter Script for more details.

Is it possible to remove empty query string parameters in jMeter?

I want to test an endpoint using jmeter, that has a copule of query string parameters, one of which is optional, loading the values from a CSV file. The problem is, can I avoid sending the query string parameter if I don't have a value for it?
It is but it will require some Groovy coding
Add JSR223 PreProcessor as a child of the request which query string you want to modify (or according to JMeter Scoping Rules if you want to apply the approach to more than one request)
Put the following code into "Script" area:
def newData = new org.apache.jmeter.config.Arguments()
0.upto(sampler.getArguments().size() - 1, { idx ->
def arg = sampler.getArguments().getArgument(idx)
if (!arg.getValue().equals('')) {
newData.addArgument(arg)
}
})
sampler.setArguments(newData)
That's it, the PreProcessor will be executed before the HTTP Request sampler and will remove all arguments which don't have their respective values

How to conditionally extract data with the if controller after sampling?

I am extracting the HTML response code from a samplier. I would like to use the if controller to conditionally extract more information if the right response code is returned.
So teh Get Message Response Extractor would save the response code to the variable: GetMessageResponse.
Then the If Controller would check if GetMessageResponse is 200:
If this is true then extract more information like this:
However I am not getting anything in ResponseText, what am I doing wrong?
You can do it in one shot if you switch to the JSR223 PostProcessor, the relevant Groovy code would be:
import com.jayway.jsonpath.JsonPath
if (prev.getResponseCode() == '200') {
def responseText = JsonPath.read(prev.getResponseDataAsString(),'$.MessageObj.Text').get(0)
vars.put('ResponseText', responseText)
}
else {
vars.put('ResponseText','Response code is: ' + prev.getResponseCode())
}
References:
Jayway JsonPath
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
In JMeter what you do is extract whatever the response and set Default Value field to something that will be filled when response will not contain extraction, for example for JSON Extractor:
What you show will not work because you put Extractors in IfController, as there is no Sampler, nothing will happen due to scoping rules.
Also when you'll need for another thing to use If Controller, no need to extract response code, just use:
${JMeterThread.last_sample_ok}

Variables and data files in Postman Collection Runner

I have an API get requests in Postman that uses a data file of voucher codes to look up other information about the code, such as the name of the product the code is for. When using collection runner the voucher codes are passed incorrectly and the data is returned about the product.
For some reason, I'm unable to capture the data from the response body and link this into the next request.
1st get request has this in the body section:
{
"dealId": 6490121,
"voucherCode": "J87CM9-5PV33M",
"productId": 520846,
"productTitle": "A Book",
"orderNumber": 23586548,
"paymentMethod": "Braintree",
"deliveryNotificationAvailable": true
}
I have this in the tests section to capture the values:
var jsonData = pm.response.json()
pm.environment.set("dealId", jsonData.dealId);
pm.globals.set("productId", jsonData.productId);
when posting the next request in the body:
{
"dealId":{{dealId}},
"dealVoucherProductId": {{productId}},
"voucherCode":"{{VoucherCode}}",
}
and pre-request scripts:
pm.environment.set("productId", "productId");
pm.globals.set("dealId", "dealId");
As you can see I've tried to use global and environmental variables both are not populating the next request body.
What am I missing?
This wouldn't set anything in those variables apart from the strings that you've added.
pm.environment.set("dealId", "dealId");
pm.globals.set("productId", "productId");
In order to capture the response data and set it in the variable you will need to add something like this to the first requests Tests tab:
var jsonData = pm.response.json()
pm.environment.set("dealId", jsonData.dealId);
pm.globals.set("productId", jsonData.productId);
Depending on the response schema of the first request - This should set those values as the variables.
You can just use the {{dealId}} and {{productId}} where ever you need them after that.
If you're using a environment variable, ensure that you have created an file for those values to be set.

JMeter Get Body Data before Sending

for example i have http body data like this
{
"signature" : "${signatureCreate}",
"paramA" : "1A02",
"paramB" : "aaa",
"paramC" : "asass"
}
how could i get all params (paramA, paramB, paramC) in my BeanShell Preprocessor? i have to get all these 3, encrypt it, and put it back in "signature" param
i also tries using JSR223 PreProcessor like this (only to try to get the paramA value, but still no luck)
def body = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put("signatureCreate", body.paramA);
thanks in advance
Your code should work fine (given you choose Groovy in the "Language" dropdown), just add Debug Sampler and View Results Tree listener to your Test Plan, you will be able to see signature variable value in the "Response Data" tab of the Debug Sampler
More information: How to Debug your Apache JMeter Script

Resources