0
votes

I am writing my variables to a CSV file but I am facing these 2 issues- 1. Even if the script finishes, the CSV file displays it is in use and read only mode 2. I want to have tab separated values but \t in following does not work

FileWriter fstream = new FileWriter("C:\\path to csv\\test.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("num1"));
out.write(System.getProperty("\t"));
out.write(vars.get("num2"));

out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

What do I do to fix them?

2

2 Answers

1
votes

Alternatively, you can try writing something like the one mentioned in this blog. Maybe it helps.

import org.apache.jmeter.services.FileServer;

// Get the variable(s) from the JMeter script
tempVar = vars.get("ExampleVar");

// Static elements or calculations
part1 = "Car Speed is: ";
part2 = " km/h";

// Open File(s)
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\carSpeed.csv", true); 
p = new PrintStream(f); 

// Write data to file 
p.println( part1 + tempVar + part2 );

// Close File(s)
p.close();f.close();
0
votes

Replace this line:

out.write(System.getProperty("\t"));

with

out.write("\t");

This causes your script failure on that line hence "num2" variable isn't being written and stream is not closed.

Next time you experience a problem with Beanshell script take a look into jmeter.log file for any errors. If you want a human-readable stacktrace - surround your code with try/catch block like:

try {
    //your code here
}
catch (Throwable ex) {
    log.error("Something wrong", ex);
    throw ex;
}

See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using and troubleshooting Beanshell scripts.


By the way, you can write any JMeter variable into .jtl results file using Sample Variables property, in order to enable it just add the next line to user.properties file (lives in JMeter's "bin" folder)

sample_variables=num1,num2

and the values will be added to .jtl file.