Jmeter: variable in JSON file is not changed by extracted value - jmeter

I have 2 requests:
1) creating new publication
2) updating it
After the first call I extract $reportId from JSON response. In second request I define a file with JSON data in 'Body Data': ${__FileToString(update_report_json_request.txt,,)}
Inside the file I add extracted reportId:
{"sourceuri":"db://reports/${reportId}"}
But value is not being set, and content from file is sent as it is. How can I pass extracted previously value to the file used for POST data?

Add eval function wrapping the result:
${__eval(${__FileToString(update_report_json_request.txt,UTF-8,)})}
eval function returns the result of evaluating a string expression.

Related

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

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.

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.

Extracting a value from jmeter response and exporting it to a csv file

I need to know how can I extract a value from response in jmeter and export it to a csv file.
Suppose my response is like this:
<ns3:UpdateConsumerResponse
xmlns:ns3="http://Pandora.NextGenCRM.Integrations.UpdateConsumerResponse"
xmlns:ns0="http://tempuri.org/"
xmlns:ns1="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Request"
xmlns:ns2="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Response">
<MasterConsumerID>
CRM-CONID-000000519344
</MasterConsumerID>
</ns3:UpdateConsumerResponse>
I need to extract the master consumer value and export it to an csv file.
First of all you have to add an regular expression extractor as a child of this request
and then mention below inputs
1.Reference Name: MasterConsumer (or any variable)
2.Regular expression: abc(.*?)d (suppose your value is like abcCRM-CONID-000000519344d
then provided reg ex will work, now replace abc with your left
boundary and d with right boundary which you can get from your response. if still you need more help then
please provide this value along with more text from both side )
3.Template: $1$
4.Match No:1
5.Default Value: null
now you have your value stored in MasterConsumer variable (apply debug sampler to verify). Just you need to write into csv file, so add beanshell post processor as a child of same request and write below code for printing data into csv file
MasterConsumer =vars.get("MasterConsumer");
f = new FileOutputStream("Path"-Output.csv",true);
p=new PrintStream(f);
this.interpreter.setOut(p);
p.println(MasterConsumer);
f.close();
Add XPath Extractor as a child of the request, which returns above value
Configure it as follows:
Check Use Namespaces box
Reference Name: anything meaningful, i.e. ID
XPath query: /ns3:UpdateConsumerResponse/MasterConsumerID/text()
Add Beanshell PostProcessor after the XPath Extractor
Add the following code into the PostProcessor's "Script" area:
import org.apache.commons.io.FileUtils;
String file = "path_to_your_file.csv";
String ID = vars.get("ID");
String newline = System.getProperty("line.separator");
FileUtils.writeStringToFile(new File(file),ID + newline, true);

JMeter Nested Variable Reference with JDBC Resultset Variable and Counter Variable

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.

Resources