Do more with your JMeter scripts
Published Oct 18 2022 10:00 PM 2,817 Views

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:

  1. Add the thread group and two HTTP request samplers to the test plan.
    one JSON extractor
    one JSR223 PostProcessor element SamplerList.png

     

  2. 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.idextraction.png

     

     

  3. 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.
    Spoiler

    In 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.

    inlinescript.png

     

  4. Save the JMeter script and add it to your test plan. 
  5. 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.Azure Load Testing LogAzure Load Testing Log

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:

  1. In the Apache JMeter GUI, open the test plan.
  2. Define the filename and the filepath first using the user defined variables element within the test planFile Path and Filename as ParametersFile Path and Filename as Parameters
  3. Reference it in the Script file field of the JSR223 element.parametrized File path and File Nameparametrized File path and File Name
  4. Save the Test plan and then upload the test plan along with the script file, in this case Test.groovy when creating the test.testplanupload.png

     

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.

  1. Add the external script file to the `configurationFiles` setting.configyaml.png

     

  2. 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!

 

3 Comments
Version history
Last update:
‎Oct 25 2022 09:09 AM
Updated by: