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:
There are broadly two ways to script within a JMeter test plan:
External scripts referenced within JSR223 elements
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.
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.
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.Azure 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.