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.
Related
I'm trying to wrap my head around Jmeter functions and how to actually use them in an HTTP JSON request. All examples I've found on function use them as titles in HTTP requests, which is not helpful.
All I want to do is this, and then use that variable in my POST request. But that doesn't work and I have no clue why not.
...
"dates": {
"invoiceDate": ${testdate},
...
In all examples I've seen, the function is set as the HTTP sampler name, like so:
This is not helpful at all since I can't use that as a variable.
I don't think 20211 is the valid year, do you mean 2021?
If you put the function inside User Defined Variables - it will be evaluated only once and won't be random, you should rather inline it into the request body as User Defined Variables are evaluated only once, when the test starts
If you're trying to post JSON - I think you should surround the function with quotation marks, otherwise the JSON will be invalid. Suggested change:
{
"dates" : {
"invoiceDate": "${__RandomDate(,,2021-06-20,,)}"
}
}
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
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.
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}
I need to query a MYSQL Database for a list of siteIDs and siteURLs. I've specified these names in the JDBC Request's Variable Name field.
Then I created a ForEach Logic Controller to cycle through the siteURLs ${siteURL_1} till the last record from the result as such:
Input Variable Prefix: siteURL
Start Index: 0
End Index: 40
Output Variable Name: newSiteURL
Then I use this in the HTTP Request's Path field as:
${newSiteURL}
This works fine and the HTTP requests are going through.
Now, I want to name the HTTP Requests properly so that they are indexed better.
For that, I decided to use the siteID field from the result set.
To do that, I created a counter variable as such:
Start: 1
Increment: 1
Maximum: 40
Reference Name: siteIndex
Now, to get the siteID from the result show in the corresponding HTTP Request, I edited the Name field of the HTTP Request to this:
${siteID_"({siteIndex})"}
But my HTTP Requests in the View Results Tree still end up showing as:
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
...
And not the actual siteID for the corresponding siteURL in the HTTP Request like:
21231
12315
21654
64574
76876
...
You need to change this bit:
${siteID_"({siteIndex})"}
to
${__V(siteID_${siteIndex})}
Explanation:
As per __V function documentation
For example, if one has variables A1,A2 and N=1:
${A1} - works OK
${A${N}} - does not work (nested variable reference)
${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1
See Using JMeter Functions post series for more examples on how to get things done with useful JMeter Functions.