Friday, August 21, 2020

Jmeter Assertions - JSON Assertion

If you don't know what is assertion in Jmeter and its types, I would suggest you to read my previous post here.

2. JSON Assertion

The JSON Assertion is used to validate a JSON response. To add JSON assertion in the test plan, refer the below snapshot (Right click on Thread Group -> Add -> Assertions -> JSON Assertion).

Let us see an example how to use JSON Assertion. I am going to hit the URL "https://reqres.in/api/users?page=2" which gives the below response.

{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name":"Lawson","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"},{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg"},{"id":9,"email":"tobias.funke@reqres.in","first_name":"Tobias","last_name":"Funke","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"},{"id":10,"email":"byron.fields@reqres.in","first_name":"Byron","last_name":"Fields","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"},{"id":11,"email":"george.edwards@reqres.in","first_name":"George","last_name":"Edwards","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"},{"id":12,"email":"rachel.howell@reqres.in","first_name":"Rachel","last_name":"Howell","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"}],"ad":{"company":"StatusCode Weekly","url":"http://statuscode.org/","text":"A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things."}}

Example 1: I would like to check the "total_pages" value is 2 from the server response. If it is not equal to 2, then the sampler should fail.

So the option I choose: Assert JSON Path exists "<This should be the path of "total_pages">" and check "Additionally assert value" option and expected value is "2"

Response for the sampler is shown below. The expected value "2" is available for total_pages in the response. Hence, the assertion is passed.

Now let us change, the expected value to 10 in JSON Assertion and see the result of the sampler.

Response for the sampler is shown below. The expected value is "10"  for total_pages, but actual value present in server response is "2". Hence, the assertion is failed.

Example 2: I would like to check the "first_name" value is not null from the server response. If it is null, then the sampler should fail.

So the option I choose: Assert JSON Path exists "<This should be the path of "first_name" from the data array>" and check "Additionally assert value" option, check "match as regular expression" option and expected value is "([^"]+)".
Response for the sampler is shown below. The expected value is not null for first_name of data array in the response. The actual captured value from the server response is "Micheal" which is not null. Hence, the assertion is passed.

Example 3: I would like to check the "first_name" value is not "Michael" from the server response. If it is "Michael", then the sampler should fail.

So the option I choose: Assert JSON Path exists "<This should be the path of "first_name" from the data array>" and check "Additionally assert value" option, check "Invert Assertion" option and expected value is "Michael".
Response for the sampler is shown below. The expected value is not Micheal for first_name of data array in the response. The actual captured value from the server response is "Micheal". Hence, the assertion is failed.
NOTE: The key is here to find the JSON path for the expected value. If you are not sure about the JSON path, you can use the online tool like JSON Path Finder.
Simple. Isn't it?

No comments:

Post a Comment

Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'. Again, we are using the sample file named &#...