Case Study - API automation using Postman
Posted By -
Suchita Gondhane Date: 12/07/2019
Project Challenge:
One of the project need was to test around 30+ APIs for every bi-weekly release. The number of APIs would gradually grow as the development team continued developing new features every sprint. Initially tried to test these manually, however testing all the endpoints manually before every release was time consuming and tedious task.
We proposed and decided to move ahead with API automation using Postman to reduce the time taken by the redundant work and also help us have proper result/report structure in place.
What is Postman:
Postman is used for API (Rest API services) testing / automation. Apart from testing API manually we can use Newman for API automation in Postman. Newman is a powerful command-line collection runner for Postman. It allows you to run and test a Postman collection directly from the command-line which is very helpful when you want to integrate it with your CI/CD tools like Jenkins.
Setting up postman with newman
-
Get the latest postman app from here
-
Set up Newman as per the instructions here
Let’s go through few basic Postman concepts
Variables: Postman supports different types of variables like Global, Environment, Data, Local, etc. We have used Environment and Data variables in our case.
-
Environment variables: Environment variables are a set of key-value pairs. We can either use static or dynamic variable which can be used throughout the collection. For example: In our collection, we have created one dynamic variable called ‘token’ which saves the unique id parsed from response generated during user creation and use the same variable for user details Update. Apart from dynamic variables, we have used static variables for environment URLs which store the application URLs of stage or production environment.
-
Data variables: The Collection Runner lets you import a CSV or a JSON file, and then use the values from the data file inside HTTP requests and scripts these variables are called as 'data variables'. We have used the data variable to read the values from a test-data file (csv). In the user creation scenario instead of hard coding the input values, we read the data from a CSV file and stores the values in the data variables for further usage.
Collections: The collections are nothing but a set of requests or endpoints within a folder. Collection mainly includes the Endpoints, Test, Pre-request and assertions.
-
Endpoints: An endpoint is a URL pattern used to communicate with an API
-
Test: It includes assertions
-
Assertions: We used assertion to assert on the API’s response if it matches with expected result or not. In our script, we have used the Chai – Assertion Library.
Below are a few examples which shows how to use assertions to a postman test :
-
After user creation we added the assertion to check that user name present in data file matches with user name present in the response:
pm.test("Username is correct", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql(data.name);
});
-
Assume that the expected response of an endpoint is 200. We can verify the response through assertion if it’s matching with the actual or not.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Pre-request: Pre-request is the script that executes before the endpoint execution.
We can use the output of pre-request response as input for an API in test.
Please note that pre-request script can be added to a collection, folder or a single API request.
For example: We have a user registration endpoint which requires some keywords to be
passed on along with the user name, email etc. We did not have the option to pass on the keywords directly
as they get generated dynamically. So in our pre-request script we made a call to the keyword endpoint,
parsed the response and used the data in the user creation endpoint. See the code snippet for the pre-request described above:
pm.sendRequest(pm.variables.get("URL") + "/keywords/fastsearch?query=basic&limit=50", function (err, response) {
var jsonData = response.json();
console.log(jsonData);
if (jsonData.items.length > 0) {
postman.setEnvironmentVariable("keywordId4", jsonData.items[3].id);
postman.setEnvironmentVariable("keywordValue4", jsonData.ite ms[3].value);
postman.setEnvironmentVariable("keywordId5", jsonData.items[4].id);
postman.setEnvironmentVariable("keywordValue5", jsonData.ite ms[4].value);
}
});
Collection Runs: We can run collection either manually using collection runner Or schedule the script using Postman Monitor Or via Command Line using Newman.
-
Collection Runner: We can execute the entire collections via collection runner Or a few selected endpoints. The endpoints in the collection are executed sequentially. Collection runner also provide us the option to select the environment, number of times (iterations) we want the collections to run and many other options.
-
Postman Monitor: With the help of Postman Monitor, we can run the collections periodically to check for its performance. When we set up a monitor, Postman server hits the endpoints in our collection according to the specified frequency.
-
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. It is built with extensibility in mind so that one can easily integrate it with continuous integration servers and build systems. So ideally we have 2 options to run the collections using Newman.
-
Command-line: We can start the script directly from the command line and at the end of the execution see the result in the Postman HTML report. See one of the HTML reports generated by POSTMAN below:
-
Integration with Jenkins: We can create a job in Jenkins which will execute the collection in Postman via shell command which contains nothing but the newman command line parameters.
Conclusion:
-
Postman is an API automation tool which is very easy to setup and execute.
-
It does not expect users to be expert with any programming language. A very basic understanding of JS functions (for pre-request script) and assertions writing is enough.
-
Without much efforts, it generates a readable HTML report.
-
Script execution can be scheduled in iteration using Postman Monitors.
-
Can be easily integrated with Continous Integration tools like Jenkins.