Write Response to File in JMeter
JMeter provides range of features to carry out effective performance testing. Apart from all the features provided out of the box, it also lets us create custom solutions for our specific requirements. Recently we encountered a need to extract a specific part of response data and write it in an external file. While this may look a daunting task at first, it turned out to be relatively easy by utilizing some JMeter components. We have written this tutorial to show how we can do this. We will write response to file in JMeter. But first, let’s take a look at the scenario in question.
Scenario:
We wanted to retrieve email and access token from response data and write them both in a CSV file for later use. We used Regular Expression Extractor for fetching email and access token from received response data. Now, to write them in CSV file, we’ll need a simple BeanShell script.
Solution:
- Write a regular expression to extract email from response as below.
- Write a regular expression to extract access token.
- We now have email and access token values stored in ’email’ and ‘access_t’ variables respectively.
- Add BeanShell PostProcessor under HTTP Request sampler in JMeter.
- Write below code in BeanShell PostProcessor.
12345678token = vars.get("access_t");email = vars.get("email");f = new FileOutputStream("PATH TO CSV FILE", true); //specify true if you want to overwrite file. Keep blank otherwise.p = new PrintStream(f);this.interpreter.setOut(p);print(email + "," + token);f.close(); - Run your test. A CSV file will be created containing email and access token at the location that you have specified in BeanShell script.
So this is how we write response to file in JMeter. Now that you have this setup working on your system, let us know in comments how we can refine it further.
Hello Maharshi,
I tried this but didn’t get any CSV file created. This is the code in my BeahShellProcessor
token = vars.get(“access_t”);
email = vars.get(“email”);
f = new FileOutputStream(“C:\jmeter\apache-jmeter-3.0\bin\Sample Tests\”, true); //specify true if you want to overwrite file. Keep blank otherwise.
p = new PrintStream(f);
this.interpreter.setOut(p);
print(email + “,” + token);
f.close();
Please what I’m I doing wrong?
I’m working on remote client machine for script development , i used above code to write data to external file but file got created and not able to write the data.
I see below exceptions in logs:-
error in method invocation :method set output(java.io.stream) not found in class bsh.interpreter.
error in method invocation :method set output(java.io.printstream.) not found in class bsh.interpreter.
It did not work for me. I wanted to store token only.
If we follow the above mentioned process,we will be able to get only the email and access token of one user to CSV file,
How can we save the multiple users Email and Access_token to the Single CSV file.
The second parameter is an append, according to java description:
append – if true, then bytes will be written to the end of the file rather than the beginning
you have to set it to true. The comment in the article is wrong!
Hi, I am able to get the data in csv file:
Following is the code
token = vars.get(“accessToken”);
f = new FileOutputStream(“D:\\git\\2FA_Performance\\src\\test\\resources\\Token\\abc.csv”, true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(token);
print (FileServer.getFileServer().getBaseDir()+”\\carSpeed.csv);
f.close();
I am getting null in my csv what is the issue.
Thanks in advance!
Hi,
How to write Regular Expression. I have following Body of Request from which I need email:
POST data:
{
“firstName”: “fname6”,
“lastName”: “lname6”,
“email”: “1510305844315_10-11-2017_1@jmeterapitest.com”,
}
and from following response data I need memberId
{“status”:”Success”,”statusCode”:”100″,”statusMessage”:”Request processed successfully”,”errors”:[],”detail”:{“amountCharged”:309.0,”memberId”:”FL75068460″}}
Use the below code to write variables to a file.
name = vars.get(“name”);
email = vars.get(“email”);
log.info(email); // if you want to log something to jmeter.log file
// Pass true if you want to append to existing file
// If you want to overwrite, then don’t pass the second argument
f = new FileOutputStream(“/my/file/path/result.csv”, true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(name + “,” + email);
f.close();