How to use Postman for API test automation. Part II – Validate Response

postman-320x132

In a previous part of this small tutorial we have installed Postman and executed our first test. This test is just validating a response code and response time. This time we will try to write some more extensive tests that will validate a response and make sure that result is stable. This is critical, especially if your API is integrated with remote agent that are hard to reinstall. So you have to make sure that from release to release response structure is the same.

We will be working with the same swapi.co service that we have used in a previous part. Let’s try to write a test that will validate a structure of response and make sure that if response structure changes we will know about it.

So we have this response right now : Screen Shot 2017-08-08 at 7.03.55 PM

In order to add some test, you need to go to “Tests” section under your request as in our previous exercise.

As you can see we have a lot of ready to use snippets on a right in or test and we will use some of them to create more comprehensive test then we have right now.
Screen Shot 2017-08-21 at 8.37.51 PM

So let’s start. We will use “Response body: JSON value check” snippet and modify it to server our needs.
Let’s add this few lines of tests now :
// Validate response keys
var jsonData = JSON.parse(responseBody);
tests["Count is present"] = ("count" in jsonData);
tests["Next element is present"] = ("next" in jsonData);
tests["Previous element is present"] = ("previous" in jsonData);

Your test should look something like this now :
Screen Shot 2017-08-21 at 9.55.20 PM

If we send our request once again, we will see that we have 5 tests now and all of them are passing. So what we just did ?

  • Parsed JSON response from API and stored it into jsonData var jsonData = JSON.parse(responseBody);
  • Validated that our response body has key called “count” : tests["Count is present"] = ("count" in jsonData);
  • Validated that our response body has key called “next” : tests["Next element is present"] = ("next" in jsonData);
  • Validated that our response body has key called “previous” : tests["Previous element is present"] = ("previous" in jsonData);

Ok, from this example, I hope you have got a basic idea on how to validate a simple request for it’s structure, what about more complex JSON structures ? In our response we have a key called “results”. This is an array of planets, that have movies this planets were mentioned in and people that visited this planets.

Let’s try to validate this structure as well now. The idea, is to run over all elements in our “results” array and validate if all of elements of this array have all keys that they need to have.

Add this part to your tests and send request once again :

// Store results in a separate var
var results = jsonData['results'];
// Validate validate each element of array for all keys
for (i = 0; i < results.length-1; i++) {
tests["Name is present"] = ("name" in results[i]);
tests["Rotation Period is present"] = ("rotation_period" in results[i]);
tests["Orbital Period is present"] = ("orbital_period" in results[i]);
tests["Diameter is present"] = ("diameter" in results[i]);
tests["Climate is present"] = ("climate" in results[i]);
}

At this point your tests should look like this :
Screen Shot 2017-08-21 at 10.11.23 PM

What we just did by adding this few lines of code to our tests :

  • Created a separate variable to store results only var results = jsonData['results'];. This is not really needed as we could just use a jsonData but it is just easier to use extra var in this case and specially if you have more complex structure.
  • Created a loop that will go over all results in “results” variable for (i = 0; i < results.length-1; i++) {}
  • Validated if current element in result has a key name : tests["Name is present"] = ("name" in results[i]);
  • Validated if current element in result has a key rotation_period : tests["Rotation Period is present"] = ("rotation_period" in results[i]);
  • Validated if current element in result has a key orbital_period :tests["Orbital Period is present"] = ("orbital_period" in results[i]);
  • Validated if current element in result has a key diameter : tests["Diameter is present"] = ("diameter" in results[i]);
  • Validated if current element in result has a key climate : tests["Climate is present"] = ("climate" in results[i]);

As you can see we were able to validate a JSON structure with a few simple steps and now can be sure that when we test our API it will has all necessary keys in your response.

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.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s