top of page
Writer's pictureAnn Samuel

Postman Unboxed: Variables, Tests, Environments and Chaining Explained - Part 1

Why Postman?


Postman is an application for testing web APIs seamlessly. It enables the user to send requests to the server and get the response. 


  1. It makes testing APIs hassle-free with its well structured and user-friendly interface. 

  2. It has the ability to setup test data, construct an HTTP request with custom payload and assert for values in the response


More about Postman:


Postman is not just limited to send , receive requests & responses but it has a lot more features: 


  1. Using variables

  2. Pre-scripts and Tests

  3. Chaining API requests

  4. Data Driven Testing

  5. Environments 

  6. Collection Runner 

  7. Newman in command line

  8. Postman monitors

  9. Workspaces and how to use them

  10. Visualize json response

  11.  Flowcharts

  12. Mock servers


Prerequisites:

The only prerequisite for this blog is having a basic familiarity with APIs. If you are not familiar with API basics, make sure to check out API HTTP methods so that you understand what API is and how it works.


Some good-to-know information:

1. Some HTTP requests like GET requests and DELETE requests do not require a request payload. Hence while sending GET requests and DELETE requests, we do not provide a request body.

2. When we enter a URL inside the Postman, it immediately captures the query parameter and displays it on the screen

3. The following things need to be validated when we are performing API testing: Status Code, Time Required To Respond, Size Of Data, Body of Response, Cookies created during the response & Headers of Response


Lets deep dive in now to understand more about the features listed above


Using variables

           Variables are used to store data or value. The stored data can be reused in requests. It can be called throughout the environment, collection and request based on the scope of the variable provided. This will help to update the variables in one place rather than updating in every request.


Postman supports the following variables scope:

Global - Global variables have broader scope and can be accessed between collections, environments, requests. Global variables are available throughout the workspace.

Collection - Collection variable scope is limited to collection and API’s in that collection.

Environment - Environment variables are limited to the particular environment such as development, testing and production.

Data - Data variables comes from external csv or JSON to set value which can be used in collection runner and newman

Local - Local variables are limited to API request or collection. These are temporary variables and will no longer be available after execution




Creating pre-scripts and tests:

Tests and pre scripts are written in the Tests/ Pre-request Script Tabs. This code is written in java script and it is very human  readable. 


The workflow of the postman flows as below:




The pre-script tab enables the user to do any processing needed before sending a request, like setting variable values. Any code here, runs before the request is sent


Scenario:

As a user I want to set a random customer name to an environment variable before I post a request.

//set environment variable for customer name
pm.environment.set("customerName", "test" + parseInt(Math.random()*1000));

The Tests tab allows for any post-processing after a request is sent and includes the ability to write tests for assessing response data. Postman provides code snippets which automatically generates code to validate the response. 


Scenario:

As a user i want to confirm endpoint return status as 200 and assert for valid json body

//confirm endpoint return 200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
//create test and assert for valid json body
pm.test("response must be valid and have a body", function () {
     pm.response.to.be.ok;
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});

How to chain API requests

Chaining of API’s means extracting a value from one API and passing that value to the other API’s. Chaining is done to eliminate the manual effort while handling dynamic values which are referred to in other API’s. It basically helps in automating API collections

To do this, we’ll integrate Javascript code in our Test section. We now have to set up our environment variables with test data which we expect to be returned and pass these values in other API requests to test the response.


Scenario:

As a user i want to extract a value and set to environment to chain the value to other API’s

//extract a value and set to environment to chain API request
const response = pm.response.json();
pm.environment.set("orderId", response.orderId);

Data Driven Testing:

Data driven testing helps to validate the API request with bulk data upload. In postman we can do data driven using external csv or JSON file. 

We need to provide the file that contains the data sets for the variables in the collection runner, select environment, file type and no. of iterations to run the collection runner. Postman will iterate through your data file, replacing variables with values from each row, and execute the request for each set of data

JSON file


collection runner


Run results


Setting up environments:

Environments are multiple hosts or servers in which your api’s are deployed. This is particularly useful when you need to switch between different environments, such as development, testing, and production. Postman provides a way to create multiple environments and use them as per users requirement.


We can create an environment by giving the environment name (eg., QA), adding variables (eg., baseUrl) and the initial and current value (eg., api.qa.com). Save the environment and switch between environments. This will update the values of the variables used in the request.




In Postman Beyond Basics - Part 2 (https://www.numpyninja.com/post/postman-beyond-basics-unveiling-collection-workspaces-visualization-flowcharts-and-mock-servers), we'll expand on collection, workspaces, visualization, Flowcharts and Mockservers.

67 views

Recent Posts

See All
bottom of page