Dynamic Response Handler in Jmeter

Reading Time: 4 minutes

Hi Folk, Let’s move ahead with respect to the Jmeter blog series for new problem statement solutions. So in this blog, we will see how can we handle dynamic responses and also handle dynamic request failures in Jmeter scripts.

Basically, handle dynamic response means Correlation which is the process of capturing and storing the dynamic response from the server and passing it on to subsequent requests. A response is considered dynamic when it can return different data for each iterating request, occasionally affecting successive requests. Correlation is a critical process during performance load test scripting because if it isn’t handled correctly, our script will become useless.

As I already explained how we can extract dynamic response data in the jMeter test in my earlier blogs. Click here to check. Here firstly, we will look to handle dynamic response using post Processor- BeanShell postprocessor, JSR223 postprocessor and then handle failures in the request using postprocessor ‘Result Status Action Handler’.

1. Dynamic response handler using Beanshell Postprocessor-

Let’s take an example that we have HTTP requests in the JMeter and it returns different data in response to each iterating request in json format that is also called correlation. Now we have to extract some value from the JSON data response and pass that response in subsequent requests.

Response data-

{
	"accessToken": "ZjljN2NhM2MtY2Q3OC00YTdjLTgwNjktNTUxYTE1MmY5MGRl",
	"expiresIn": 2406,
	"refreshToken": "OWVjODVhOTYtMzNjNy00NzgxLTgzYzQtMWVmODlkYmUyN2Fl",
	"tokenType": "Bearer"
}

 Now we have to extract the access token from this response. These are the steps need to follow- 

How to add this Postprocessor

  • Add the JSON jar file in the lib folder of apache JMeter- jar if it is not already there.
  • Add Beanshell Postprocessor in your HTTP Request
  • Right-click on Https Request→ Add→ PostProcessor→ Beanshell PostProcessor.
  • Add Debug Sample to view the value of extracted data.
  • Right-click on ThreadGroup → Add→ Sampler→ Debug Sampler.
  • Add this code to extract the value of accessToken in BeanShell Postprocessor.
import org.json.JSONObject;
 
String response_data = prev.getResponseDataAsString();
JSONObject data_obj = new JSONObject(response_data);
String token = data_obj.get("accessToken").toString();
vars.put("token", token);
log.info("++++++++++++"+token);

2. Dynamic response handler using JSR223 Postprocessor-

Similarly, we can use the JSR22 postprocessor to handle the dynamic response in JMeter. We can use JSR22 postprocessors with any scripting language. So if anyone is not comfortable with beanshell scripting then JSR22 is the best post processor to handle the dynamic response. 

Let’s move with the same example of response data. Here we will use javascript to extract the JSON data from the response. These are steps to add JSR22 Postprocessor in the JMeter script.

var response = prev.getResponseDataAsString();
 responseJson = JSON.parse(response);
var token = responseJson.accessToken;
vars.put("token", token);
log.info("++++++++++++"+token);

How to add this Postprocessor

  • Add JSR22 Postprocessor in your HTTP Request
  • Right click on Https Request→ Add→ PostProcessor→ JSR22 PostProcessor.
  • Add Debug Sample to view the value of extracted data – Right-click on ThreadGroup → Add→ Sampler→ Debug Sampler.
  • Add this code to extract the value of accessToken in JSR22 Postprocessor.

3.  Dynamic Request Failure Handler-  

Sometimes in JMeter, requests are failed while running tests. So the result of the failed request does not give any response. Subsequently, another request is failed because it is using some extracted dynamic data in request from the response of the above-failed request. Now it is difficult to check the performance of the request API. So we need to stop the failed iteration in the test.

Here is Jmeter we have a Result Status Action Handler postprocessor.

 This JMeter element is used as a post-processor to take the action in case of failure of the request. There are certain options given which instruct the JMeter thread on what to do next?

How to add this Postprocessor –

  • Right-click on the HTTP request.
  • Click on Add→ Postprocessor→ Result Status Action Handler

In Result Status Action Handle Pane, choose Stop Test Now. This selection will stop the test if JMeter gets the error from the server response.

Action to be taken after a Sampler error:

Actions taken in case of sampler error occurs, either because the sample itself failed or an assertion failed, etc. The possible choices are-

1. Continue: To ignore the error and continue with the test

2. Start Next Loop: To ignore the error, start the next loop and continue with the test

3. Stop Thread: It is to stop the current thread and exit the thread

4. Stop Test: The entire test is stopped at the end of any current samples.

5. Stop Test Now: The entire test is stopped abruptly. Any current samplers are interrupted if possible.

6. Break Current Loop: To exit from the existing loop.

7. Go to the next iteration of Current Loop: To start the next iteration

So this is how we can handle dynamic response using postprocessors and extract data. If you have any questions and queries related to Jmeter, you can ask in the comments. I’ll provide the solution asap.

I hope you enjoyed it and it helped you!! stay connected for more future blogs. 

Thank you!!  

References:

https://jmeter.apache.org/usermanual/component_reference.html

Knoldus-blog-footer-image

Written by 

Aditi is Sr. Software Consultant -QA having experience of 3 years in Software Testing. She is familiar with API Testing & Performance Testing Using the tools like Jmeter,Postman, ReadyAPI(SoapUI),LoadUI are her expertise .Also having Knowledge of core concepts of manual and automation both as basic of Selenium Webdriver.She is always ready to adopt any environment and zeal to learn the new tools & technologies