0

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.

Devika
  • 21
  • 1
  • 6

2 Answers2

1

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
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

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")
Ori Marko
  • 56,308
  • 23
  • 131
  • 233