Advanced Features of Postman
Author : Geetu Sadana
Postman is a powerful tool used to test web services. It offers a simple user-friendly interface. User just needs to enter the request URL, fill in the necessary headers and body, select the HTTP method, and hit the ‘send’ button.
List of various advanced features of postman
- Collection runner
- Data Driven Testing in collection
- Global Variables
- Assertions
Collection Runner
Collections are a great way of storing requests in postman. They are basically tools for arranging pre-built requests that can be organized into folders and can be easily exported and shared with others. This way we can centralize and organize all the required info for each request.
How to create collection and execute some of the basic operations using collections
- When we open Postman. In the left, we will see below screen
- New Collection folder with a plus sign is visible, which can be clicked to create a collection.
- Give the name of the collection and click on create.
- We need to add requests in collections. On the right hand side of the send button there is a save button dropdown.
- Click this save as the screen below will open. Give the name of the request and select collection and save.
- Then the request will be saved to collection. Similarly, we can save all requests in one collection or can create multiple collections.
Running collection using collection runner
- Click on the arrow icon on the right side of the collection name.
- When we click on this arrow icon, below screen opens up
- Here click on the Run button. Collection runner windows will open up.
- Select your collection and click on the Run button. We can save responses.
- Add delay in between the request and also specify the iterations we require.
- We will get below screen after collection has been run successfully.
Data Driven Testing in collection
Validating API responses with different requests is usually a manual process most of the time. This coupled with multiple combinations of inputs might become a complex task and it is not maintainable on every addition of new field or modification in test data.
Decoupling test data with actual API test scenarios helps to maintain the test.
So, modifying or adding any data fields will just need updating the data files.
Data-driven Testing is the mechanism of getting testing data from different files. Data can be stored in different data storage applications such as Excel.
To get the data from a different file, first we must create variables and then the data stored in the file can be referred.
STEPS:
- Create a CSV file with topRow (and topColumn) value as “Email” [This represents the value of data variables that will be replaced by postman with the actual data rows.]
- Now in the Request body add reference of this variable
- Go to “Runner” in Postman and select the CSV file and execute the collection.
- Iteration will depend on the number of rows in CSV of that reference variable.
Global Variables
Problem Statement
We often encounter different servers in our company or team. These can be either development servers, a production server, testing server. Every server has different types of request APIs. Since we know that a collection can include many requests within it, what if the URL changes? For example, teams change their server request URL. For running requests successfully now we have to make the changes to each and every request. For 300 requests, we will have to change 300 times. This is purely a waste of time and resources. To overcome the same, Postman has a feature named as global variable.
Variables allow us to store and reuse values in requests and scripts. By storing a value in a variable, we can refer to it throughout collections, environments, and requests — and if we need to update the value, we only have to change it at one place. Using variables increases your ability to work efficiently and minimizes the effort. Basically a variable is a symbolic representation of data that allows us to access a value without entering it manually every time. This can be useful especially if we are using the same values at multiple places.
How to create a Global Variable in Postman
- Click the Environment quick look (eye button) in the top right of Postman and click Edit next to Globals.
- Add a variable name and give it an initial value — click Save and close the environment modal.
- Open a new request tab and enter the URL with variable reference. Hover over the variable name, we can see the value.
Assertions
Assertions make Postman a great utility tool to create integration tests for rest API endpoints. Depending on the output of these assertions, we can see if a test can either pass or fail.
Assert is validating an exception to check if a specific test has passed or not and it is done inside the “Tests” tab for a request.
Tests ensure that API is working as intended. We can write and run tests in Postman for each request.
As our codebase grows, we want to make sure it’s not breaking anything that was previously working fine. The higher test coverage, the more flexible and bug-resistant code will be, and the less time we will spend in debugging.
The easiest way to get started with writing tests in Postman is to take a look at the snippets on the right side of the Tests tab. Clicking on a snippet will append the JavaScript code into the editor. We can also write our own assertion using postman functions.
Tests are scripts written in JavaScript that are executed after a response is received.
Request-> Response-> Test Script
To enable assertions usage, postman provides us with pm.test() function. This function requires the first parameter as the name of test and second parameter as the callback function where we add assertion with functions like pm.expect(), pm.response() etc.
We can see the results of the test on the bottom, under the corresponding Tests Result tab in the response viewer.
When running a collection using the collection runner in the Postman app, we can view tests running and the results in real time.
Advantages of Automating API using Postman
- Postman is easy to use with ease of test development and effort can be saved in automating API via postman collection.
- Ability to incorporate Java scripting , assertion etc.
- Store information in an organized way and helps in documenting all APIs at one place which can be referred in future.
- Integrates with build systems, such as Jenkins using the Newman command line tool easily.
- Postman makes it easy to share and move tests into different systems by exporting test collection. This also makes it extremely simple to use code repository tools such as Git or SVN to manage the Postman tests.
- Ability to have connection with the database using drill. One can easily perform backend testing without writing much code.
- We can automate APIs On-the-fly while doing testing without any extra effort.
- Postman also provides you with tons of code snippets with examples if you are new to writing test scripts.
Other blogs in this series -
Newman :
Newman is a command line Collection Runner for Postman. It allows you to run and test a Postman Collection directly from the command line. To read more about Newman refer here.
Apache Drill for DB Connection in Postman :
Looking into building up an end to end solution for API testing. Postman being a rest client not a DB client was a big challenge. So we need a Middle ware to communicate from Postman to DB directly. So this can be resolved using the open source SQL query engine Apache Drill. To learn more about this, refer here.
Codeless API Automation and Monitoring System using Postman :
This blog describes the technique to automate APIs using Postman instead of Rest Assured and describes the advantages of using Postman over Rest Assured. To read more about this, refer here.