Search This Blog

Breaking

Friday, 9 April 2021

[Jmeter] -> How to put assertion in response body parameters?

 Recently, we faced a situation wherein our performance test, we need to put an assertion on one of the response body parameter field values. We googled about it but could not find any straightforward answer.

I posted the question in StackOverflow as well but unfortunately could not found the solution from there also. Later, got a solution from one of my colleague who has expertise in Jmeter.

https://stackoverflow.com/questions/66989810/jmeter-less-than-or-greater-than-assertion-in-jmeter-assertion-on-api-respon

So, to put an assertion in the response body parameter, first, you need to find out that value from the response body. So for this purpose, we first added a Post -Processor: JSON -Extractor.




Inside this, we first fetched the value and stored this value inside a JMeter variable:

queryTime = $.metadata.queryTime



Later, we added a JSSR23 assertion to put our assertion condition.


And there we added our code for validation:

String jsonString = vars.get("queryTime");
int length1=jsonString.length();
String Qtime1=jsonString.substring(0,(length1-2));
int time = Qtime1.toInteger()
log.info ("The queryTime is " + time);

if (time>1000)
{
AssertionResult.setFailureMessage("The queryTime is taking more than 1000ms");
AssertionResult.setFailure(true);
}


The same assertion later came in a result like below:


If you need this field value in summary report excel, you will have to add this variable in user.properties file which will be available in the bin folder.






No comments:

Post a Comment