Devika
Devika

Reputation: 31

JMeter Beanshell Postprocessor : Reading a file

I am using the below script in a Beanshell Postprocessor

import java.io.*;
File f =new File ("C:\Users\xxxxx\Desktop\testresults.csv");
FileWriter fw=new FileWriter(f,true);
BufferedWriter bw=new BufferedWriter(fw);

var r=prev.getResponseCode();

if (r.equals("200"))

{
    bw.write("Test Passed");
    }

    else
    {
        bw.write("Test failed");
        }
bw.close();
fw.close();

But I am getting the below error BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.io.*; File f =new File ("C:\Users\xxxxx\Desktop\testresults.csv") . . . '' Token Parsing Error: Lexical error at line 2, column 23. Encountered: "U" (85), after : ""C:\".

What could cause the above error.

Upvotes: 0

Views: 731

Answers (2)

Dmitri T
Dmitri T

Reputation: 168147

You need to escape a backslash with a backslash like:

C:\\Users\\xxxxx\\Desktop\\testresults.csv

or use a forward slash instead:

C:/Users/xxxxx/Desktop/testresults.csv

A couple more hints:

  1. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
  2. If you run your test with 2 or more concurrent threads they will be writing into the same file resulting in data corruption due to a race condition so maybe it worth considering switching to Flexible File Writer instead

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58862

Change to JSR223 Post Processor and write as one line (groovy default)

new File("C:\\Users\\xxxx\\Desktop\\\testresults.csv") << (prev.getResponseCode().equals("200") ? "Test Passed" : "Test failed")

Upvotes: 0

Related Questions