Application programming interfaces, more commonly known as APIs, have become central to various applications, from social network interfaces to online shopping. However, handling and testing APIs can be clumsy and often contain errors when done manually. Here, API automation comes into the picture. Isn’t it easier to use API testing tools like Postman, which can automate the testing of repetitive tasks, manage the API, and even develop the report simultaneously?
In this blog, we will discuss how you can easily set up API automation in Postman, perform end-to-end Automated testing, and generate advanced HTML reports to ensure that your APIs are operating at their best.
At the current period, it is rare to come across applications that do not use APIs & therefore, we have APIs active in our lives as they enable applications to work in the background and deliver value addition to the actual user experience that is greatly valued in today’s world. So, what is an API? An API is an acronym for application programming interface, a collection of protocols that facilitates interaction between various software applications. Developers leverage APIs to overcome those gaps and help implement the efficacious, robust, protectant, and performant applications users want.
APIs function within application-to-application, system-to-system, and device-to-device data exchange. This is done using the request and response model. The request is made to the API, where it is received. The API fetches the data and then passes it back to the user. The API client begins the conversation workflow by requesting the API server. The request can be initiated in many ways. For example, users may start an API request with a search query or a button click.
External events, such as a notification from another application, may also trigger API requests. The API client sends the request to the API server, responsible for handling authentication, validating input data, and retrieving or manipulating data. Finally, the API server sends a response to the client, which includes components like the status code, Response Header, and Response Body.
Postman is an API platform development and testing framework that offers numerous conveniences. It enables developers to write, document, and publish API requests and collections, test and simulate APIs, and monitor and analyze APIs. It also features collaboration and documentation, making it easier for developers to work in teams and share information about APIs.
For API automation purposes, we have a locally hosted JSON server, which has two endpoints: to create posts and to get posts by ID or all posts.
In the example above, you can see that we are calling the post endpoint using the POST method to create the record. It has a JSON body.
{
“title”: “{{title}}”,
“author”: “{{author}}”
}
When testing the API manually, we usually pass actual values for the fields provided in JSON. It is recommended that you test the API manually first before automating it. We have already tested it manually, and now that we want to automate it, we have replaced the values with variables.
When we run automation, values for these fields are read from an Excel file called data-driven automation testing.
The following API is going to fetch the record for the created post.
This endpoint fetches the post record based on the ID provided. It uses the GET method to retrieve the record and returns 200 status codes in case of success. Here, we have also passed the id variable instead of passing the actual value for the same purpose of automating it.
To pass data from one response of one request to another request, we need to do the following:
As visible from the above images, the response to the create post API has an ID field, which is required to send the second request to fetch the record. So, we wrote code in the scripts tab of the first request that will create an environment variable called id and store the field response received from the first request. You must create an environment first to store the environment variable.
As you can see, we have created a “Test” environment and an id variable with a value received in response to the request.
When we fire the second API to fetch the record, it will pass the ID field value received from the first request and retrieve the record created with this ID, displayed in the image above.
In the above example, we covered several essential concepts, such as declaring variables and using post-request scripting to chain the requests and validate the data and status codes. Now, let’s see how to test the automated suite that we have created using Collection Runner.
When you open the collection runner, it will show you the APIs that will be executed and the order in which they will be executed on the left-hand side. On the right-hand side, there will be settings that you need to make according to your needs for implementing the automation suite.
Important things to notice are that the data file should be in UTF-8 CSV format, and the iteration count will be automatically updated based on the number of data rows in the file. You can provide a delay between 2 requests during the execution.
The image below shows the execution result for each iteration and the details for each API request.
To run API collection in CLI mode, you need to install several plugins as mentioned below:
1. Newman
2. Newman-reporter-HTML
Please ensure that you have a stable version of node and npm already installed on your system. Open CLI and execute the following commands:
$ npm install -g newman
$ npm install -g newman-reporter-HTML
$ npm install -g newman-reporter-htmlextra
Once installation is complete, you need to export the following from Postman.
1. Collection: you can export the collection from Postman by clicking on the “Export” option on collection settings as a JSON file, or you can also use the Collection URL by choosing the “via API” option while exporting the collection
2. Environment: You must export the environment file by selecting an Export option in Environment settings.
Once you are done with export, you can execute the collection using Newman as shown below:
This will generate an HTML report without much formatting, as displayed below:
You can view more CLI execution reports & advanced HTML reports using the command mentioned below:
As you can see in the image above, the report is well formatted. It gives more insight into the execution by showing a summary, total requests executed, Failed Test, & Passed Test with granular details.
Fully automated API suites can help developers and testers save effort and time when working on a project with frequent release cycles. Teams can also integrate these automated suites into CI/CD pipelines. Using this approach, developers can quickly identify if any breaking occurs due to a code change or if the release is good enough to continue testing further.
SPEC INDIA, as your single stop IT partner has been successfully implementing a bouquet of diverse solutions and services all over the globe, proving its mettle as an ISO 9001:2015 certified IT solutions organization. With efficient project management practices, international standards to comply, flexible engagement models and superior infrastructure, SPEC INDIA is a customer’s delight. Our skilled technical resources are apt at putting thoughts in a perspective by offering value-added reads for all.
“SPEC House”, Parth Complex, Near Swastik Cross Roads, Navarangpura, Ahmedabad 380009, INDIA.
“SPEC Partner”, 350 Grove Street, Bridgewater, NJ 08807, United States.
This website uses cookies to ensure you get the best experience on our website. Learn more