Search code examples
jmeterbeanshell

jmeter beanshell call jmeter function


I have a HTTP request sampler configured. In the request body, I call out to a beanshell function I wrote:

${__BeanShell(createHeader("GET"\,"Customer"\,"${__UUID}"\,"${__time(yyyy-MM-dd'T'hh:mm:ss)}"))}

The function just builds some request strings with the passed-in parameters. I want to remove the jmeter function calls (__UUID and __time), and call them directly from within the beanshell function. I could not find a way to do that. Thanks


Solution

    1. Don't inline JMeter Functions or Variables into scripts, in your case you will have to go for code-based equivalents instead, to wit:

      • __UUID() -> UUID.randomUUID().toString()
      • __time() -> new java.text.SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())
    2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, in your case it would be __groovy() function. If you want to re-use createHeader method - you can put it into a separate .groovy file and define groovy.utilities property pointing to this file.

      See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests.