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 ";
1
  • Your errors message doesn't correspond to the code that you've provided . . .
    – ernie
    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.

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.

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.