Search code examples
testingjmeterjsr223

how to use __eval in preprocessor of jmeter


I am using a faker.jar plugin to generate random data inside json file

The JSON file is like

{
    "name" = "${__BeanShell(new com.github.javafaker.Faker().name().fullName())}"
	"id" = "${__BeanShell(new com.github.javafaker.Faker().IdNumber())}"
}

and in jmeter i am using a http sampler

enter image description here

This is working fine, but now i want to evaluate the request with response, so i want to save the request body inside a variable and pass that variable to http request and then use post process to evaluate request against response, but i am unable to use ${__eval()} function inside preprocessor.

How to fix that


Solution

    1. Add JSR223 PreProcessor as a child of the HTTP Request sampler
    2. Put the following code into "Script" area:

      vars.put('foo', sampler.getArguments().getArgument(0).getValue())
      

      3 That's it, the PreProcessor will fetch the body data from the HTTP Request sampler and store it into ${foo} JMeter Variable.


    Be aware that according to JMeter Best Practices you should be using __groovy() function instead of __Beanshell() so consider amending your JSON file on next available opportunity.

    Be also informed that JMeter provides some built-in functions allowing generation of random values i.e. __Random(), __RandomString(), __UUID(), etc. so you might even discard your 3rd-party library as JMeter built-in functions provide better performance.