JMeter – How to save a variable to a file?

Sometimes you need to save the value of a variable to a text or CSV file. Such a scenario is especially required at the time of test data generation when a value exists in the response and you need to save it to an external file.

JMeter has some postprocessor elements to perform the post-request activities which also include saving a variable value to a file.

Methods to save a variable to a file in JMeter

Using BeanShell PostProcessor

  1. Add a Regular Expression Extractor to a sampler that has (dynamic) values to save to a file (Example: favicon and src)
  2. Add a BeanShell PostProcessor under the same sampler
  3. Paste the below code in the script section of BeanShell PostProcessor
import org.apache.jmeter.services.FileServer;

String path=FileServer.getFileServer().getBaseDir();

var1= vars.get("favicon");      
var2= vars.get("src");

f = new FileOutputStream("C://Project/Scenario/testdata.csv",true);
p = new PrintStream(f); 

this.interpreter.setOut(p); 
p.println(var1+"," +var2);

f.close();

In the above code, ‘favicon’ and ‘src’ are two variables and their values will be saved in the ‘testdata.csv’ file which is located at ‘C://Project/Scenario/’. You can change these values as per your test script.

If you have to save the value of only one variable then use the below code.

import org.apache.jmeter.services.FileServer;

String path=FileServer.getFileServer().getBaseDir();

var1= vars.get("favicon");  
    
f = new FileOutputStream("C://Project/Scenario/testdata.csv",true);
p = new PrintStream(f);
 
this.interpreter.setOut(p); 
p.println(var1);

f.close();

Using JSR223 PostProcessor

  1. Add a Regular Expression Extractor to a sampler that has (dynamic) values to save to a file (Example: favicon and src)
  2. Add a JSR223 PostProcessor under the same sampler
  3. Write down the below code in the script section of JSR223 PostProcessor
import org.apache.commons.io.FilenameUtils;

var1 = vars.get("favicon");
var2 = vars.get("src"); 

f = new FileOutputStream("C://Project/Scenario/testdata1.csv", true);
p = new PrintStream(f);

p.println(var1+","+var2);

p.close();
f.close();

In the above code, ‘favicon’ and ‘src’ are two variables and their values will be saved in the ‘testdata1.csv’ file which is located at ‘C://Project/Scenario/’. You can change these values as per your test script.

If you have to save the value of only one variable then use the below code.

import org.apache.commons.io.FilenameUtils;

var1 = vars.get("favicon");

f = new FileOutputStream("C://Project/Scenario/testdata1.csv", true);
p = new PrintStream(f);

p.println(var1);

p.close();
f.close();

So, these are two simple methods in JMeter to save the value of the variables to a file.


5 thoughts on “JMeter – How to save a variable to a file?”

  1. Good work, keep posting this type of scenarios ,it helped me a lot .
    Can you please mention override the file using jsr223 post processor

  2. please provide the code which will show how to write 10 or more variables to a file, Thankyou

  3. Hi Sanjay,

    You can simply define the variables at the first i.e. var3, var4 and add those variables in the println function separated by +”,”+

Comments are closed.