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.
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.
- Write a regular expression to extract email from response as below.
- Write a regular expression to extract access token.
Fetching 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.
token = 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);
print(email + "," + token);
- 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.