JMETER | How To Write Data From Response To A CSV File
Problem Statement:
As an Automation Tester,
I want to Save Response Data to file
And I want to extract some portion of response
So that I can generate .csv file which can be utilized further
- If we understood the problem statement better then basically, we have TWO global objectives which we are trying to solve.
- Save response to file (Can Achieve by using “Save Responses to a file” listener)
- Extract few portion of response and save to .csv file. (Little complex and can’t be achieved by in-built component of Jmeter.)
Why we need Response to store?
- It’s requirement of test. We can use this data for subsequent analysis, importing to a database, and more. Responses are used to debug further environment.
- Handle too large Response — We will not be able to see a response in the View Results Tree if the size of the response is more than 200K. There are a couple of ways to solve this problem and one of them is saving the response to a file.
Solution:
API Details:
-----------------
End-point:
URLInput Param:
name = Ahmed
Let’s see step-by-step how problem statement can be achieved -
- Add basic elements to the Test plan.
* Test plan -> Add -> Thread (Users) -> Thread Group
* Thread Group -> Add ->
- Config Element -> User Defined Variables
- Sampler -> HTTP Request
- Listener -> Save Responses to a file
- Listener -> View Results Tree
- Let’s add user defined variable called name which we would like to pass to next request. (Name=Ahmed)
- Added HTTP request to get name & pass user defined variable. ${Name}
2. We are just about done! Run the script.
- As we can see, each request has a response in the View Results Tree. This is very comfortable for debugging.
- Since we have added “Save response to file” as listener, there will be separate file created under <JMETER_HOME>/Bin folder.
Where we have reached so far?
We have achieved first goal which is to save response to file. What is Next -
A) Need to extract details from response andB) Only extracted details should be saved to .csv file for further use.
A) Need to extract details from response:
- Add below elements to Existing Test Plan
* Thread Group -> Add -> Sampler -> Debug Sampler
* HTTP Request -> Add -> Post Processors -> XPath2 Extractor
- XPath Extractor to get all entities who name is “Ahmed”.
Where-* XPath query : //name
* Match No. should be -1 ( extract all results, they will be named as <variable name>_N (where N goes from 1 to Number of results))
* We are just about done! Run the script.
- The Debug Sampler generates a sample containing the values of all JMeter variables and/or properties.
- The values can be seen in the View Results Tree Listener Response Data pane.
- We can observed that all matching names are extracted and assigned to unique variable by default.
B) Only extracted details should be saved to csv file:
- Add below elements to Existing Test Plan
* HTTP Request -> Add -> Post Processors -> JSR223 PostProcessor
- Now, it’s time to write JAVA code inside JSR223 PostProcessor to get all extracted variables and write into the csv file.
import org.apache.commons.io.FileUtils
import java.text.SimpleDateFormat;// Get total number of matches. (Returns string)
def resultCount = vars.get("ExtractedName_matchNr")
log.warn 'Output to jmeter console' + resultCount// Generate timestamp to create uniue file name.
String fileSuffix = new SimpleDateFormat("ddMMyyyyHHmm").format(new Date())// Create file Object
f = new File("results_"+fileSuffix+".csv")for (int i=1; i<=resultCount.toInteger(); i++)
{
// Get each matching result one by one.
records = vars.get("ExtractedName_"+i)// Write result to csv file.
FileUtils.writeStringToFile(f,records + System.getProperty("line.separator"),true)
}
We are just about done! Run the script.
- There will be separate file created under <JMETER_HOME>/Bin folder. (EX: results_xxxxxxxxxxxx.csv)
Conclusion:
There is no direct way to save extracted response to the CSV file. Needed processors like JSR223 PostProcessor in order to achieve our objective.
Note: Probably, Given problem statement can be addressed by different ways in Jmeter which I have not explored yet. Source code(LearnCSVDataExport.jmx) is available on GitHub.