Search code examples
jmeterperformance-testingloadrunnerbeanshell

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.


Solution

  • 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