We know you want to do more with your JMeter scripts.
We came across many uses cases wherein customers wished to do more with their JMeter scripts, like ability to manipulate variables, or to use logs to debug the conditions. It is almost possible to achieve many of the use cases through the commonly available JMeter elements and augment them with the rich source of plugins available. There may be instances wherein it would be required for the test authors to script in order to manipulate the data before or after it is passed to samplers.
This blog will highlight steps on how to retrieve a user id from a JSON response and then pass it to the next request to fetch the details of a user. The details would be displayed on the log. This is a very common use case wherein it would be required to retrieve data based on extracted input from a prior request. Also, logging is a useful mechanism to debug testing scripts which is a common use case of using JSR223 elements in the test script.
There are broadly three categories of JSR223 elements supported within JMeter which are:
- JMeter Preprocessor
- JMeter Sampler
- JMeter postprocessor
There are broadly two ways to script within a JMeter test plan:
- Inline scripting
- External scripts referenced within JSR223 elements
Inline Scripting
Inline scripting refers to writing the script within the JMeter test plan itself in the designated space for scripting within the element.
To edit your JMeter script by using the Apache JMeter GUI in the inline scripting mode:
- Add the thread group and two HTTP request samplers to the test plan.
one JSON extractor
one JSR223 PostProcessor element -
Insert the values for the id that you would want to fetch.
In this scenario we are trying to fetch the first elements id from the response. - Configure the JSR223 post processor to fetch the value from the previous sampler and pass it to the next HTTP request sampler.
You can use the prev keyword to fetch the values from the prior sampler request.
To verify the value being fetched we are outputting the same to log using the log.warn method. This log method can be used to debug the script.SpoilerIn Azure Load testing you cannot alter the log levels, hence you may use the WARN level log to output and print values to the log.
- Save the JMeter script and add it to your test plan.
- Once the test is run and finished, look at the log file and verify that the postprocessor element could pass the requisite id to the next request and fetch the details of the user.
Passing an External script along with your JMeter Test plan
To be able to reference it you would need to add the below function that would retrieve path of your external Groovy or BeanShell script referenced within your JSR223 element.
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator)}
Referencing an external script file in your load test script by using the Azure portal:
- In the Apache JMeter GUI, open the test plan.
- Define the filename and the filepath first using the user defined variables element within the test plan
- Reference it in the Script file field of the JSR223 element.
- Save the Test plan and then upload the test plan along with the script file, in this case Test.groovy when creating the test.
If you run a load test within your CI/CD workflow, you can add the external script file in the configuration file section of the YAMl.
- Add the external script file to the `configurationFiles` setting.
- Save the YAML configuration file and commit it to your source control repositor. The next time the CI/CD workflow runs, it will use the updated configuration.
Go ahead and use JSR223 to enable greater flexibility with scenario construction with your JMeter scripts.
You can join our global developer community with feedback and insights here.
Happy LoadTesting!