API Automation using Postman

Hello everyone! in this article, I would like to share my knowledge about API Automation using Postman.

And if you are new to API testing, I would recommend reading the below article before you start following the API automation.

Well… back to the main topic and here come the questions. What is Postman ?

Let’s find out.

What is Postman

Postman is a REST client that provides a platform to develop, use, and test Rest APIs. It can be installed in the PC as a software tool of use as a browser extension.

Using Postman we can create collections which are consist of HTTP requests. we can write assertions using JavaScript and use those to validate the integrity of the code.

Moreover, we can define local variables and loops to run a test.

Postman supports global variables and using those variables we can store data from previously executed tests. Furthermore, we can use the same code (assertions) in multiple environments.

Since API tests bypass the UI of the application, the tester will get the quick responses, and also the code is highly maintainable than GUI tests.

Install and Set Up Postman

Postman is an Open Source tool and it can be easily downloaded and install

  1. Visit https://www.postman.com/downloads/ and select your operating system and then click Download.

2. After a successful installation, you need to sign up for Postman account

Using Postman

Overview of Postman

Creating a new collection using Postman tool

Steps to follow

  1. Either click on the “+ New collection” button or “+New” button and then click on “Collection”

2. A new pop up will appear and provide a meaningful name to the collection then click on the “Create” button

Creating a new request using Postman

  1. Click on the “…” icon on Postman tool and then click on “Add request
  2. A new pop up will appear, provide a name then click on “save to <collection name>” button

We can Click on the created request name and open it on the “Build” section of the Postman tool. We can change the HTTP method according to the request

Variables

There are two types of variables using postman, environment and global. The global variable can use for all collections. The environment variables specially structured for targeting collections as per the environment. Once we defined variables, it can use in a postman request using the below format.

{{VARIABLE_NAME}}

Ex: “title”:”{{ProgramTitle}}”,

Alright, let's do some practicals

Below I have described a few scenarios which automated using Postman.

Scenario 1: Sign in and obtain the access token

Step 1: capture the information from GUI

  1. Navigate to the GUI login screen and press F12 and then navigate to “Network” section
  2. Clear the network section and log into the GUI

According to the above screenshot, you can see the HTTP method is POST.

3. Copy the URL and paste it as a request URL on the Postman tool using the POST method.

4. Navigate to the “Request Headers” and copy the values of “Accept” and “Authorization”

Step 2: Insert those captured details on Postman request

Step 3: Define the body section

  1. Navigate to “Form data” section from GUI, and copy the Key-value pairs

In this scenario, instead of giving the key-value pairs in the request body section (as form-data) on the Postman tool, we can use Environment variables and insert values on those variables. To achieve that firstly, we need to create an environment and define environment variables on those

Step 4: Managing Environment

For Ex : As you can see I define environment variables so that in the later run I need to only change the variable value and no need of request body changing or modification

Step 5: parameterize the form Data

After defining environment variables, we can use those predefined variables in Postman request body section as Form-data

When mouse hover we can Initial and current value

Step 6: Set values to the environment variables

We can set values to the environment variable using the “Tests” section on Postman tool and using Java scripts

Furthermore, we can also parameterize the URL

For Ex:

Writing Test Scripts and Assertions

We can write test scripts and assertions in the Test section of the postman. Postman allows us to write JavaScript code which can assert on the responses and automatically check the response. We can define our own javaScript code or use the Snippets to generate the code.

This code checks that the status of the message is 200, a success response.

Sample assertion

  1. Using the status code

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});`

2. Using the content-type header check

pm.test(“Content-Type is present”, function () {
pm.response.to.have.header(“Content-Type”);
});

If we use either one of these or both, without amending the code, and the postman will assert that the status code is 200 and display the results in the “Test Results” tab.

If pass, the test results are showing in green, and if fail, it will display in red. Moreover, It displays a number of pass/fail tests

Use of code snippets

As I mentioned earlier we can use code snippets to generate the code. There are already defined snippets for status code assertion and content-type header assertion.

by clicking the “Code” section also we can generate snippets. We can select a language and generate code from that language.

That concludes our article as well. I hope you got a picture of what we are trying to achieve. Your comments and suggestions are welcome. You may also share your experience with API test automation using Postman

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store