Search code examples
jmeterbeanshelljmeter-5.0

Error on accessing Output Variable in beanshell preprocessor in Jmeter


I am unable to print 'Output Variable' value of foreach Controller in Beanshell Pre/Post-processor in Jmeter.

log.info("inside hash"+ ${current_file} ); //current_file is the Output variable name defined in foreach controller and has the value of current file path.
File file=new File(${current_file});
byte[] content = FileUtils.readFileToByteArray(file);

Whenever I execute the tests, I get this error:

2021-12-15 19:58:25,208 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``import org.apache.commons.io.FileUtils; import org.apache.jmeter.services.FileSe . . . '' Encountered "( "inside hash" + C :" at line 4, column 9.

Can anyone help me fix this error?


Solution

    1. Don't inline JMeter functions or variables in form of ${current_file}, use vars shorthand for JMeterVariables class instance instead

      Something like:

      String current_file = vars.get("current_file");
      log.info("inside hash"+ current_file );
      File file=new File(current_file);
      
    2. Don't use Beanshell, since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, there is a chance that your code will just start working after switching to Groovy or at least you will get more informative errors.