October 18, 2020

Saving Data to CSV Files With Java Through JMeter

Open Source Automation
Test Data Management

Creating CSV files with Java through Apache JMeter™ is a convenient and easy way to form and to update your CSV files. Instead of creating the CSV file separately, you can complete your whole work process in one place - in JMeter.

Let’s get started.

1. First add a Thread Group

Right click on Test Plan -> Threads -> Thread Group

2. Add an element that enables you to write a code in Java, i.e a BeanShell element. This can be a Sampler, PreProcessor or PostProcessor, according to your script needs. In this example we will use a sampler.

Right click on Thread Group -> Sampler -> BeanShell Sampler

This is where you add the Java code with all the relevant parameters for the CSV file. The code executes writing to a CSV file with the variables in it.

update CSV files in JMeter with java

This is the code I used. This code writes the parameters that you set, to a CSV file. Every time the sampler is executed, one more line is added to the CSV file.

importjava.io.FileWriter;importjava.util.Arrays;importjava.io.Writer;importjava.util.List;//Default separatorcharSEPARATOR=',';//function write line in csvpublicvoidwriteLine(FileWriterwriter,String[]params,charseparator){booleanfirstParam=true;StringBuilderstringBuilder=newStringBuilder();Stringparam="";for(inti=0;i<params.length;i++){//get paramparam=params[i];log.info(param);//if the first param in the line, separator is not neededif(!firstParam){stringBuilder.append(separator);}//Add param to linestringBuilder.append(param);firstParam=false;}//prepare file to next linestringBuilder.append("\n");//add to file the linelog.info(stringBuilder.toString());writer.append(stringBuilder.toString());}//get path of csv file (creates new one if its not exists)StringcsvFile="";// for example '/User/Downloads/blabla.csv'String[]params={${param1},${param2},${param3}};FileWriterfileWriter=newFileWriter(csvFile,true);writeLine(fileWriter,params,SEPARATOR);//proper close to filefileWriter.flush();fileWriter.close();

 

If you want to use it, you can adjust the following lines:

  • Change the parameters here, and add more if you need to:
String[]params={${param1},${param2},${param3}};
  • If you run this script a few times, “true” will append new lines to the file. To override the existing lines, change to “false”. This is an optional part of the code.
FileWriterfileWriter=newFileWriter(csvFile,true);

 

4. Add a BeanShell element that will read the entire CSV file and set each parameter to a variable so they can be used later on in the script.

Here is an optional script for reading the entire file, you can create your own.

importjava.io.BufferedReader;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;StringcsvFile="";BufferedReaderbufferedReader=null;Stringline="";StringSEPARATOR=",";try{bufferedReader=newBufferedReader(newFileReader(csvFile));intcounter=1;while((line=bufferedReader.readLine())!=null){String[]items=line.split(SEPARATOR);for(inti=0;i<items.length;i++){vars.put(“param”+i+counter,items[i]);}}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{if(bufferedReader!=null){try{bufferedReader.close();}catch(IOExceptione){e.printStackTrace();}}}

 

Let's explain some of the lines:

  • The following line goes through each line in the CSV file and reads it.
while((line=bufferedReader.readLine())!=null)

 

  • The following defines the array of the CSV line's params.
String[]items=line.split(SEPARATOR);

 

  • The following part sets a new JMeter variable for each parameter in the line. You can define the parameters according to your needs and decide what to do with them in JMeter.
items.length;i++){vars.put(“param”+i+counter,items[i]);}

 

That’s it! Now you can incorporate this configuration into your entire script, and integrate the CSV file reading and writing into your complete test flow.

That’s it! You now know how to read and write data to CSV files through JMeter in Java.

You can learn more JMeter for free through BlazeMeter University.

START TESTING NOW

 

Related Resources