Go Green One tree
One life
Trees
Loading...

Mastering API Automation with Postman From Setup to Advanced HTML Reports

Author
SPEC INDIA
Posted

October 28, 2024

Updated

December 10th, 2024

Category Testing

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.

API automation with postman

What is an API?

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.

Understanding Various API Models

  • REST: REST is the most popular API architecture for transferring data over the internet. In a RESTful context, resources are accessible via endpoints, and operations are performed on those resources with standard HTTP methods such as GET, POST, PUT, and DELETE.
  • SOAP: The concept used by SOAP, an abbreviation for Simple Object Access Protocol, is that XML is used to pass highly structured messages between a client and a server. SOAP is mainly applied in enterprises or some old models, though it contains several security mechanisms as its disadvantage; it is comparatively slower than other API structures.
  • GraphQL: GraphQL is an open-source query language that enables clients to interact with a single API endpoint to retrieve the exact data they need without chaining multiple requests. This approach reduces the number of round trips between the client and server, which can be helpful for applications running on slow or unreliable network connections.
  • WebHooks: Webhooks support events where some request is propagated automatically by an event happening. For example, when a particular event is tripped in an application, say a payment has been made, the application makes an HTTP POST request to a pre-set Webhook URL with the event details in the request’s body. The event that is received by the webhook can then be worked on by the system, and the right action can be taken.
  • gRPC: RPC stands for Remote Procedure Call, and gRPC APIs originated from Google. In gRPC architectures, a client can call on a server as a local object, making communicating easier for distributed applications and systems.

What is Postman?

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.

Understand How to Automate REST APIs Using Postman & Create an End-to-End Automated Suite

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.

Post method

 

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.

Data driven testing

The following API is going to fetch the record for the created post.

Get method

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.

Chaining Requests

To pass data from one response of one request to another request, we need to do the following:

  • Send the first request
  • Check the response fields and values
  • Create a variable in the script tab and store the field response in the variable

Chaining request

Post response

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.

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.

Test environment

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.

Execute Automated Suites in Postman GUI using Collection Runner

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.

run sample api collection

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.

Sample api collection

All test

Configure Postman plugin for HTML report & execute Suite in CLI mode

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:

Collection using newman

This will generate an HTML report without much formatting, as displayed below:

generate an HTML report

You can view more CLI execution reports & advanced HTML reports using the command mentioned below:

CLI execution

Newman dashboard

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.

Conclusion

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 author logo
Author
SPEC INDIA

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.

Delivering Digital Outcomes To Accelerate Growth
Let’s Talk
Delivering Digital Outcomes To Accelerate Growth
Let’s Talk

Let’s get in touch!