Search code examples
functionjmeterbeanshell

Why does my function in Beanshell don't work? | JMeter


I use a Beanshell-Sampler to write a file in Jmeter. And I tried to use some functions like:

status = ${__property(status)};

or

${__time(EEE\, d MMM yyyy)};

it just worked with the Webdriversampler but not in Beanshell. Is it not possible to use a function in Beanshell or is just an import missing?


Solution

  • You can inline functions in Beanshell script, but you need to use quotation marks like:

    String status = "${__property(status)}";
    String date = "${__time(EEE\, d MMM yyyy)}";
    

    By the way, is isn't recommended to use Beanshell scripting so if you have possibility to amend JMeter installation(s) it's better to consider JSR223 Test Elements and Groovy as a language instead.

    If you decide to switch to JSR223 and Groovy - you should stop referring variables and functions as ${...} and start doing it from Groovy code like:

    Accessing property value:

    String status = props.get("status"); 
    

    Getting current date:

    import java.text.SimpleDateFormat;
    
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
    String date = sdf.format(new Date());
    

    See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for Beanshell vs Groovy benchmark, details on installation of Groovy scripting engine support in Jmeter and scripting best practices.