Simple Data Writer: Configure parameters Vs user.properties file - jmeter

As I run my tests in Non-GUI mode, I am using Simple data writer with .CSV file option to write the results. I need to see the response data only when a request fails. I open the result file after the test is complete using view results tree, synthesis report and etc. but the response data says "Non-TEXT response data, cannot record: ()".
I am trying to understand:
When to use CSV and when to .jtl option? What is the major difference? When I used CSV, I am not able to see the response data. Do I need to add the below lines in the user-properties file:
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.response_data.on_error=true
jmeter.save.saveservice.output_format=xml
OR/AND configure the parameters in the GUI (in Simple data Writer) by choosing 'Save response data' and 'Save as XML' and unselect 'save field names (CSV)'?

JMeter cannot save non-text responses into XML files, you will get Non-TEXT response data, cannot record: () message when SampleResult.getDataType() function returns anything but text
You can work it around by adding a JSR223 Listener and using the following Groovy code there:
if (!prev.isSuccessful()) {
new File('response.txt').bytes = prev.getResponseData()
}

Related

JMETER: Need to send all entries in a CSV file to HTTP request body in 'one' request

I'm trying to solve a test data issue in Jmeter. Can anyone of you have a look at below problem statement and advise here please ?
Requirement: Need to send all entries in a CSV file to HTTP request body in 'one' request to the end point.
Example CSV File:
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
HTTP Request Body:
["${data}"}]
When the data is substituted, I should be able to get below output.
[
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
]
Problem Statement: When I use CSV data set config. file, I'm unable to concatenate all entries into one single request body. My understanding is, CSV data set config. the file is not the right option here.
Did some search in StackOverflow and followed a method to achieve above using JSR223 PreProcessor' and the link is, How to send multiple json body using jmeter?.
Followed the above link and tried added below custom code provided.
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
Upon running, I get below error message,
Caused by: java.io.FileNotFoundException,
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d" (The filename, directory name, or volume label syntax is incorrect)
If the file is not found, then how come the entries are read and displayed in the log viewer?
Also, how do I link the output of custom code to the request body? Or is it taken care of by the custom code itself?
You have an error in your code, change this line:
builder.append(new File(line).text).append(System.getProperty('line.separator'))
to this one:
builder.append(line).append(System.getProperty('line.separator'))
If you want to send the full contents of the file you don't even need to go for scripting, you can use __FileToString() function right in the "Body data" tab of the HTTP Request sampler:
${__FileToString(/path/to/plans.csv,,)}
And last but not the least if you need to generate JSON from the plain text it might be a better idea to go for JsonBuilder class, see Apache Groovy - Why and How You Should Use It and Apache Groovy - Parsing and producing JSON
Two steps:
Add a User Parameter Pre-processor before the HTTP request:
Name: DATA
User_1: ${__FileToString(/path/to/plans.csv,,)}
Add the following to request body:
${DATA}

How can I dynamically post Request body (xml) and validate the response (xml)?

Is there a way to send the XML request dynamically and validate the XML response?
My scenario is:
I will have a CSV dataset config and inside the csv file I will have two column, the first one is for the inputXMLFilePath and the second column is the expectedXMLResposneFilePath.
So I need to have a JSR233 PreProcessor under HTTP request sampler, read the input file path convert it to the post body, and also has another JSR233 sampler for load the expected response from the expectedXMLResponseFilePath and compare it with the previous XML response. I have a snippet for JSON which is working fine. but for XML how can I do it?
You can use __FileToString() function for both use cases:
To send the XML request body, like ${__FileToString(${inputXMLFilePath},,)} (where ${inputXMLFilePath} is the variable from the CSV Data Set Config)
To validate the response using Response Assertion configured like:
Field to Test: Text Response
Pattern Matching Rules: Equals
Patterns to test: ${__FileToString(${expectedXMLResponseFilePath},,)}
You can use JMeter Functions literally at the any place of your Test Plan so their flexibility is higher than for other test elements. Also JMeter Functions are being compiled into native Java code therefore their execution speed will be higher and footprint will be less comparing to Groovy scripting.
Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.

How to write HTML response in a file using JMeter

Can any one help me how to save HTML response (including screen images) from View Results Tree listener using JMeter?
I can store the results in csv but my main objective is to store the screen images that are displayed in view results tree
The screenshot name should be stored under step name (eg: TC002 Account Menu)
You can add JSR223PostProcessor and save response body to the file.
For example like this:
File file = new File(pathToYourFile);
FileWriter fstream= new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(prev.getResponseDataAsString());
out.close();
fstream.close();
If you want each answer to be saved in a different file, you will need to add code to create the files and add them a new uniq name.
UPD
One way to save each response in your own file is to generate the name depending on the value of the counter like this:
(Using JMeter Functions)
def filename = "${__counter(FALSE,)}" + "response.html";
File file = new File("C://JmeterResultFolder//"+filename);
or this:
(Using Counter Sampler)
def filename = "${counter}" + "response.html";
File file = new File("C://JmeterResultFolder//"+filename);
and in the end you will get file for each request
You can user .csv file to store the response data. Please refer below screen
Basic link for Listeners
You can configure JMeter to store response data if it is needed for any reason, add the next lines to user.properties file:
jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.response_data.on_error=true
and restart JMeter to pick the properties up. Next time you run your script response data will be inlined into .jtl results file and you will be able to see it with View Results Tree listener.
More information:
Configuring JMeter
Apache JMeter Properties Customization Guide
Results File Configuration
Be aware that storing response data causes massive disk IO overhead so use it wisely (i.e. for tests development and/or debugging only) as it might ruin your test given more or less high load.

How to edit javascript response in 'save response to a file' in jmeter?

I am getting rest call response in javascript format.I could save whole response in a local file.but I have to save a part of response that is a javascript variable's value.
Please find my javascript response data:
In the below code I want to save the value of the s0.responseTextJson and need to parse the json string before saving into the file.
var s0=new Response();s0.api=null;s0.ckStatus=200;s0.ckStatusText="OK";
s0.responseText="{\"com.response-message\":{}}";
s0.responseTextJson="{\"com.response-message\":{}}";
s0.responseTextXml="<ns4:response-message></ns4:response-message>";
s0.responseType='JSON';
dwr.engine._remoteHandleCallback('0','0',s0);
Please click here to see the screenshot of actual response in jmeter
Add a Regular Expression Extractor as a child of the request which returns above JavaScript and configure it as follows:
Reference Name: anything meaningful, i.e. s0.responseTextJson
Regular Expression: s0.responseTextJson="(.+?)";
Template: $1$
Add the next line to user.properties file (lives in "bin" folder of your JMeter installation)
sample_variables=s0.responseTextJson
See Sample Variables User Manual entry to learn what's going on here.
Restart JMeter to pick the property up.
Run your JMeter test in command-line non-GUI mode like
jmeter -n -t test.jmx -l result.jtl
Once your test is finished you will see the new column called s0.responseTextJson in your .jtl results file, it will hold the relevant value for each HTTP Request sampler
I could solve the issue in above question just by adding a BeanShellPostProcessor with below script.
String originalResponse = new String(data);
String filteredResponse =
originalResponse.substring(originalResponse.indexOf("s0.responseTextJson=\"")+21, originalResponse.indexOf("\";s0.responseTextXml=\"")).replace("\\\\","#temp#").replace("\\","").replace("#temp#","\\");
prev.setResponseData(filteredResponse.getBytes());
Please click here to see my BeanShellPostProcessor in Jmeter

Regular Expression to read csv file on post processor

i have one requirement, it might be simple but not getting any clue. Actually in my jmeter script i am getting one http response as csv file. The same file i need to read into my post processor script which is groovy script. Any one has any idea how to do this, struggling from past couple of days but not getting any clue.
If I correctly got your use case, you have:
HTTP Sampler which is hitting some CSV file
Some Post Processor in which you want to read data from this CSV file
If you look into View Results Tree listener into your request, which retrieves CSV file you should see this CSV file contents.
JMeter provides powerful BeanShell Post Processor which is capable of interacting with JMeter Context. It's as simple as next line:
vars.put("myCSV",prev.getResponseDataAsString());
Another option is to store response data as a new CSV file as follows:
FileOutputStream out = new FileOutputStream("my.csv");
out.write(prev.getResponseData());
out.flush();
out.close();
As the result of above you'll store your response as file "my.csv" in the folder, from where you launched JMeter (usually /bin folder of your JMeter installation).
After that you will be able to use "my.csv" file in CSV Data Set Config to iterate through the variables as usual.

Resources