Code for Creating File in Jmeter: Jmeter is one of the open-source tools for performance testing with the help of creating performance scripts. performance scripts are created in a way as real user behaves, Jmeter is not only used for load testing, Jmeter is also used for generating test data before running the test.

This tutorial will help you with code for creating a CSV file in Jmeter or it will help you in appending the data to a single file.

In some scenario’s like tracking orders using order no or invoice number we have two ways of getting test data

  • Getting the data from the database directly without any additional task.
  • Creating a Jmeter script for generating test data for doing a load test

How many ways of creating a file in JMeter?

  • BeanShell(pre, post(processors), Sampler)
  • JSR 223  (pre, post(processors), Sampler)(Use java programming language)

Copy-paste the below code in any sampler respective to your script & whether it may be pre or post-processor or sampler

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

BufferedWriter bw = null;
FileWriter fw = null;
String filname="filename.csv";

String data="\n"+vars.get("variableName");
File file = new File(filname);
// if file doesnt exists, then create it
if (!file.exists()) {
	file.createNewFile();
}
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
  • By default, if you have not specified the complete path the file will be created in the bin folder of JMeter home directory
  • If you specify the complete path to create a file then it will create in the specified path

Object access to get the write into the file

  • If your variable which you want to write into the file is Correlated value, user-defined value then use vars Object to get the variable name
  • If your variable which you want to write into the file is a property then use Props object get the variable  name
  • If you writing a post and you want to access complete response of the sampler use sampler object to get it data for more reference use JMeter sampler API

Thanks

Also Read: Beanshell Assertion in Jmeter 

Code for Creating File in Jmeter

One thought on “Code for Creating File in Jmeter

  • August 28, 2021 at 8:48 am
    Permalink

    How to create bulk documents for bulk .docx files upload in meter?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *