1
votes

I am new to jmeter,and in my company we are doing webservices testing using jmeter.My requirement is i am using csv data config file for web services testing and here i want to update each row of csv file with test results and i am using beanshell postprocessor,please help

Ex: csv contains:

Test Case,Dates,Numbers,Results
Total test cases are 16 and i want to update as below for each test case
TC_001,9-03-2016,001,PASS
TC_002,9-03-2016,0002,FAIL

and so one...

Result = "FAIL";
Response = prev.Get....();
If(Response.Contains("Valid"));
Results="PASS";
f = new FileOutputStream("/tmp/oders.csv", true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(Results + "," + Result);
f.close();
P.Close();
3

3 Answers

2
votes

It might not be the best idea as:

  • You may have problems with re-using this file
  • You may run into a situation when 2 or more threads will be concurrently writing to the file and it will result in data loss, file corruption or IO Exception

I would recommend adding your "Test Case", "Dates" and "Numbers" variables to JMeter's .jtl results file instead using Sample Variables property like:

  1. Given you have 3 variables: TestCase, Date and Number
  2. Add the following line to user.properties file (it is located under /bin folder of your JMeter installation)

    sample_variables=TestCase,Date,Number
    
  3. On next JMeter restart you will see "TestCase", "Date" and "Number" variable values appended to .jtl results file.

It is also possible to pass the setting as a command-line parameter like:

jmeter -Jsample_variables=TestCase,Date,Number -n -t /path/to/testplan.jmx -l /path/to/results.jtl 

References:

2
votes

Add Bean shell post processor

import java.io.File;
import org.apache.jmeter.services.FileServer;  //jmeter spelling corrected
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.Writer;

File file = new File("csvFilePath");
FileWriter fstream = new FileWriter(file, true);
// true, will append the file if already exist

BufferedWriter out = new BufferedWriter(fstream);
out.write("Save your data which you want to save");
out.write("Save your data which you want to save");

out.close();
fstream.close();
0
votes

I would like to share my experience using BeanShell PostProcessor to write variables to a file.

I captured few responses from the server using "Regular Expression Extractor" and saved it to few variables Var1 & Var2 and when I wrote these variables to a file using "BeanShell PostProcessor", I have observed that it works fine when we run a test with single thread but with multi thread test, the data was being lost.

Say When I inject 100 transactions, I was able to see only 97 records in the output file.

So the best option is to set sample_variables in user.properties file so that you can get the variables out in your .jtl results file.