How to use Postman for API test automation. Part III – Pagination
Today we will talk about testing paginated data from API. The main parts of validating responses that support pagination is number of items that we get in response and page number we are at right now. This is pretty critical as when pagination does not work correctly application might suffer from performance degradation and bad user experience.
We use the same application https://swapi.co/api/planets as an example of endpoint with pagination.
As you can see we have 61 planets in total, but only 10 planets are shown on a first page. If we see a response we know that pagination works, as total number of records is bigger then records on a page and next page exists, so there are more data coming on a next page.
Now, how do we validate the same using Postman test. Let’s add a test that will validate that number of results on a page is lower then total number of results.
Let’s add this code to our postman tests :
// Response items count is less then total count
tests[“Total count is higher then page items count”] = jsonData[“count”] > jsonData[“results”].length;
Your test should look something like this now :
Execute your test an you will see that all of them pass.
So what did we check for in this test? We were validating that number of items in response id lower then total number of items.
Now let’s make sure that we have 10 items in response as expected. In order to do that add this code to your tests :
// Response items count is exactly 10 items
tests["Items count is 10"] = jsonData["results"].length == 10;
Run your tests again and you will see that now we have validated number of items that API returns for the first page.
This is all good, but now I would like to make sure that all items in response have the same schema.
In order to achieve this let’s add this code to your tests:
// Validate schema of each item in response
var results = jsonData[“results”];
for (i = 0; i < results.length; i++) {
tests[“Name in planet exists”] = (“name” in results[i]);
tests[“Rotation in planet exists”] = (“rotation_period” in results[i]);
tests[“Orbital Period in planet exists”] = (“orbital_period” in results[i]);
tests[“Diameter in planet exists”] = (“diameter” in results[i]);
tests[“Climate in planet exists”] = (“climate” in results[i]);
tests[“Gravity in planet exists”] = (“gravity” in results[i]);
tests[“Terrain in planet exists”] = (“terrain” in results[i]);
tests[“Surface water in planet exists”] = (“surface_water” in results[i]);
tests[“Population in planet exists”] = (“population” in results[i]);
tests[“Residents in planet exists”] = (“residents” in results[i]);
tests[“Films in planet exists”] = (“films” in results[i]);
tests[“Created in planet exists”] = (“created” in results[i]);
tests[“Edited in planet exists”] = (“edited” in results[i]);
tests[“Url in planet exists”] = (“url” in results[i]);
}
Execute your tests now and you will see that we have 20 test that are passing now. This is good but with a loop that we have added we should have at least 146 tests right now, if one of objects would not have a population for example test fails and we will not know which planet does not have population, as in postman we number of tests is calculated by number of uniq test names that you have. So let’s fix this problem by creating uniq test names for each object in response.
Modify your test to looks like this:
// Validate schema of each item in response
var results = jsonData[“results”];
for (i = 0; i < results.length; i++) {
tests[“Name in planet”+results[i][“name”]+” exists”] = (“name” in results[i]);
tests[“Rotation in planet “+results[i][“name”]+” exists”] = (“rotation_period” in results[i]);
tests[“Orbital Period in planet “+results[i][“name”]+” exists”] = (“orbital_period” in results[i]);
tests[“Diameter in planet “+results[i][“name”]+” exists”] = (“diameter” in results[i]);
tests[“Climate in planet “+results[i][“name”]+” exists”] = (“climate” in results[i]);
tests[“Gravity in planet “+results[i][“name”]+” exists”] = (“gravity” in results[i]);
tests[“Terrain in planet “+results[i][“name”]+” exists”] = (“terrain” in results[i]);
tests[“Surface water in planet “+results[i][“name”]+” exists”] = (“surface_water” in results[i]);
tests[“Population in planet “+results[i][“name”]+” exists”] = (“population” in results[i]);
tests[“Residents in planet “+results[i][“name”]+” exists”] = (“residents” in results[i]);
tests[“Films in planet “+results[i][“name”]+” exists”] = (“films” in results[i]);
tests[“Created in planet “+results[i][“name”]+” exists”] = (“created” in results[i]);
tests[“Edited in planet “+results[i][“name”]+” exists”] = (“edited” in results[i]);
tests[“Url in planet “+results[i][“name”]+” exists”] = (“url” in results[i]);
}
Run your tests again and you will see that we have 146 tests now and if any of them would fail you will see a planet name that does not have a certain attribute in it.
Thank you for attention and hope it was useful for someone out there!
If you like our posts please make sure you follow us to stay tuned on a new posts on test and test automation from us.