2

Can someone please help how to write data in CSV file as rows and columns?

I have below code:

import java.io.FileWriter;
import com.opencsv.CSVWriter;
// I am extracting all vars.get from XPath
Name = vars.get("AB");
Requests = vars.get("CD");
Errors = vars.get("EF");
trnxs = vars.get("GH");
CSVWriter csvwriter = new CSVWriter(new FileWriter("C:\\Users\\test\\Desktop\\J METER\\Report\\Report.csv");
String[] row = new String[]{"Name","Requests"};
csvwriter.writeNext(row);

I get an error as:

> Error invoking bsh method: eval In file: inline evaluation of:
> ``import java.io.FileWriter; import com.opencsv.CSVWriter; DealerName
> = vars.get( . . . '' Encountered ";" 
> 
> Problem in BeanShell script org.apache.jorphan.util.JMeterException:
> Error invoking bsh method: eval In file: inline evaluation of:
> ``import java.io.FileWriter; import com.opencsv.CSVWriter; DealerName
> = vars.get( . . . '' Encountered ";
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked Mar 4, 2017 at 23:41
1
  • Your errors message doesn't correspond to the code that you've provided . . . Commented Mar 6, 2017 at 2:00

3 Answers 3

1

Follow this method to write data in rows and columns:

  1. Create a notepad file out of the dat you want to pass in csv format, in this way -

    Name, Requests, Errors, trnxs
    AB,CD,EF,GH
    

    (No space betweeen the lines and no extra lines)

  2. Save the file with .csv extension. For ex: requests.csv

  3. Add a csv config element in your thread group and load this requests.csv file.
  4. Now you are good to go. You can use these values as parameters.

For ex:

//code
system.out.println("The errors are: " + ${Errors})
//code

But looking at your code, I am getting a feeling that you are either using some variables which aren't compatible with Jmeter or you havent loaded some of the required jar files. Hope this helps.

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Sep 5, 2017 at 12:09
0

First of all, remember to download this "opencsv" library and make sure it is somewhere in JMeter classpath

There are 2 approaches to get more information about your Beanshell script failure:

  1. Add debug() command to the beginning of your Beanshell script. This way you will be able to get a lot of information regarding your script flow in stdout
  2. Put your code inside the try block like:

    try {
     //your code here 
    }
    catch (Throwable ex) {
     log.error("Error in beanshell", ex);
     throw ex;
    }
    

    This way you will be able to see normal stacktrace in jmeter.log file.

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


In general I would recommend using Sample Variables property instead of your flaky approach.

answered Mar 6, 2017 at 5:30
0
import java.io.*;
import java.lang.*;
import java.util.*;
import java.io.FileOutputStream;
**//Extract all variables using Regular Expression Extractor**
Name = vars.get("AB");
Requests = vars.get("CD");
Errors = vars.get("EF");
trnxs = vars.get("GH");
log.info("Name is = " +Name );
log.info("Requests is = " +Requests );
log.info("Errors is = " +Errors );
log.info("trnxs is = " +trnxs );
f = new FileOutputStream("C:/Users/cn0494/Documents/text.csv", true);**//Give your file path**
p = new PrintStream(f); 
this.interpreter.setOut(p); 
p.println(Name+","+Requests+","+Errors+","+trnxs);
f.close();
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Dec 27, 2019 at 13:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.