Postman – Whether it is a developer, a tester or a business analyst when it comes to test the API’s, the very first thing that comes to our mind is Postman client.It is light weight, easy to install and easy to use.

Considering that you’re very much aware of the tool, we will directly jump to our main topic i.e., how to automate API’s using Postman?

Prerequisites :-

  1. Install Chrome Browser.
  2. Add Postman plugin to your browser.(By default it will install the latest plugin)

Once you install, it looks something like below.

Landing Page

Things to understand before we start writing our scripts. Numbering given in the image and its explanations are as follows

  1. Collections – Group your requests in a single folder. Easy – to maintain, to track & to execute.
  2. Interceptor – It is a chrome extension used to manipulate the requests between Postman client and web-servers. Enabling Interceptor will share the cookies among all the subsequent requests.
  3. Sync Data – Create a Postman account and make sure to Sync always so that you will have a backup in future.
  4. Sign In – To perform #3, you need to create an account and login first. Creating a postman account will also help in sharing your collections to rest of the team members.
  5. Environments – For every collection you create, you can have a environment created for it. These environments will have all the variable like, global or environment specific variables used for your collection to run.
  6. Settings – Here you can actually set/import/export/share/delete the global or environment variables. You can set these variables either manually or use set methods in pre-request-script section or in tests section.
  7. Tests – In this area, all the assertions, validations and test cases are written.

Now we will discuss what actually are these variables and what are the different types we have.

Variables are used to store your dynamic data and can be re-used for subsequent requests. It helps to keep your code clean. How? Keep reading.

Postman supports four different variables:

  1. Global variables.
  2. Environment variables.
  3. Logging variables.
  4. Data variables.

When to use these variables and what is the scope of these variables?

Global Variables :

When you want to make use of a variable irrespective of environments then set that variable as global variable using below set method. In short,  Global variables can be accessed broadly regardless of the selected environment.

Set Global variable :

Get Global variable :

Environment Variables :

When you want to create a variables, specific to that environment or you want to over-ride global variable value to a specific value only for this specific environment then make use of environment variables. In short, Environment variables can be accessed with the corresponding environment template.

Set Environment variable :

Get Global variable :

Logging variables :

Logging variables are mainly used for debugging purposes. When you send a request, after evaluation, the value of that variable will be logged into postman console. Just insert below line in your code.

Data Variables :

When you want to make use of values from a csv or a json file like firstname & lastname then import the file into the postman and postman will use these data variables from the file given.

Sample json file uploaded :

then you can make use of these values in your pre-request-script or in tests as follows:

Here data is an object which holds the values. To get the particular object property value use data.propertyName i.e., data.firstName.

If you want to make use of these values in URL parameters then write the property name in double curly braces i.e., {{firstName}} or {{lastName}} which will resolve and fetch the values of firstName and lastName as abc & xyz.

Enough theory now. Lets start the execution flow.

We will take an example here to automate an account creation POST, GET, PUT, GET(to verify updated values) & DELETE

Sample API end point to create an account :

URL is something which every request needs, it is common so why to write it for every request? we can make use of variables right? Yes!! we will create a variable and store the URL as a variable value and we will make use of that variable name in our request.

1] Navigate to Manage Environments by clicking settings(gear icon)

ManageEnvironments

2] After clicking, Manage Environments option, you will be displayed below window.

Click

3] Click Add and below window will be displayed. Enter the environment name(Test-Env), variable name(URL) and its value(https://test-sample.automation.com) to set and click ‘Add’ again.

Click-2

4] A new environment is created and inside it, variable URL is saved successfully.

Click-3

5] Select the newly created environment for your collection to run.

click-4

Now, the URL variable holds the value and hence, we can directly use that variable in our request.

1. POST Request:

To create a account, we need account details so below is the sample json body we will be using.

We may need to verify the account details in future right? So we will store these values in environment variables and will make our code reusable. This time, instead of creating variables manually, we will set it via code in ‘pre-request-script’ section like below.

 

RQ23.png

RQ22.png

Once you execute the script, these values will be resolved and will be used in the body to execute the ‘Create User’ request and along with it, these variables will be set as environment variables(refer below image) which can be used in later stages for validations.

RQ24.png

Now, we are left with ‘Tests’ section . Once the request is executed, we will be getting its response with Success message & accountID(‘data’ in below code) something like below.

To verify that the request is processed successfully, in the Tests section we will write couple of tests so that we can validate once request is executed.

Above code has 4 tests.

1.

2.

3.

4.

 

Postman UI of ‘Tests’ section will be something like below:

RQ25

and clicking ‘Send’ button, the result of the tests will be something like below:

RQ6.png

Along with this, the last two lines of code is written to store the accountID as global variable(purposely making it global instead of environment variable ) and use it for future purposes.

which will set the accountID as global variable and can be re-used for future requests.

RQ26

2. GET Request :

URL goes like this :

 

We stored the accountID in a variable and we are using the same variable as input to our GET request.

Request ‘Test’ section goes like below:

RQ8

Click ‘Send’ & Response of the request will be as follows :

Result of the tests will be as follows :

RQ7.png

3.1 PUT Request :

PUT request URL goes like this:

We stored the accountID in a variable and we are using the same variable as input to our PUT request.

Request ‘Pre-request Script’ goes like this:

RQ11

Request ‘Body’ goes like this:

RQ10

Request ‘Tests’ goes like this:

RQ12

Click ‘Send’ & Response of the request goes like this:

Test results goes like this:

RQ9

3.2 GET Request (to verify updated user details)

URL goes like this:

Tests section goes like this :

RQ13

Click ‘Send’ & Response body will be as follows :

Tests output are as follows :

RQ14

4. DELETE Request:

URL goes like this:

Tests goes like this:

Response goes like this :

Tests output goes like this:

RQ27

Now all our POST, GET, PUT & DELETE requests test cases are passing and now we will run our collection once, to see the result.

  1. Below highlighted are the set of 5 requests within a collection called TEST.

RQ28

2.  Click the arrow and click ‘Run’ button.

RQ29

3. Clicking ‘Run’ button will open a new window as below.

RQ30

4. Click ‘Run Test’

RQ31

5. Check the number of test cases passed and failed and if you want, you can download/export the test run with results as a json file too as shown below.

RQ32

Done. Congratulations!! You just learnt how to automate API’s using POSTMAN.

Before I close this topic, below are some of the key points to remember.

  • Make sure to enable interceptor always.
  • Make sure to enable Sync by logging into the Postman.
  • If a variable from the currently active environment shares its name with a global variable, the environment variable value will take priority. In other words, global variables are overridden by environment variables, which are overridden by data variables (only available in the collection runner).
  • Variables are shown in orange color and hovering on it will show the value and scope of that variable.

References :

https://www.getpostman.com/docs/