In today's fast-paced development world, API automation testing has become crucial for ensuring the quality of applications. One of the most popular tools for testing APIs is Postman. Postman is a user-friendly tool that simplifies API testing with features like creating requests, running test scripts, and automating test cases.
In this article, we’ll dive into the basics of Postman automation testing using a login page API example, demonstrating how to test a login endpoint by sending username and password details.
What is Postman Automation Testing?
Postman allows you to automate the process of API testing by writing tests using JavaScript. This makes it possible to validate API responses, handle multiple test scenarios, and integrate with continuous integration pipelines for efficient testing.
Key Features of Postman for Automation Testing
Collection Runner: Helps execute a series of requests in a sequence.
Pre-Request Scripts: Run JavaScript code before a request is sent.
Tests Tab: Write assertions to validate the response data.
Variables: Manage dynamic data such as tokens, URLs, and IDs.
Environment: Allows you to test against multiple environments like Development, QA, or Production.
Example: Automating Login Page API Testing
Let’s walk through the process of automating a simple login request using Postman. The example will send a POST request to the login endpoint with username and password, and we’ll check the response to ensure the login is successful.
Step 1: Setting up the Login API Request
Open Postman and click New > Request.
Name the request Login Request and choose the collection (or create a new one).
In the Request Type, select POST and enter the login API endpoint
In the Body tab, select raw and choose JSON from the dropdown.
e.g:
Step 2: Writing Tests for the Response
Next, we’ll write automated tests to ensure that the login request works as expected. This is where Postman’s test feature shines.
Switch to the Tests tab.
Write JavaScript code to validate the response.
Here’s an example of the test code:
This script does the following:
Checks if the response status code is 200 (indicating success).
Verifies that the response contains a token (assuming login is successful).
Validates that the username in the response is the same as the one sent in the request.
Step 3: Running the Collection
Save your request and add it to a collection.
Click the Collection Runner in Postman to run your test as part of a series of requests.
You can run it with multiple iterations if you want to test different credentials, or integrate it with environments for testing in different environments like QA or Production.
Adding Dynamic Data and Environment Variables
Postman allows you to store variables, like API keys or base URLs, so you don’t have to hard-code them in your requests.
In the Pre-request Script tab, you can define variables to be used in the request.
For example:
2. In the request body, reference the variables like so:
This way, you can manage different sets of usernames and passwords using Postman environments.
Handling Authentication Tokens
After a successful login, many APIs return an authentication token that needs to be used for subsequent requests.
In the Tests tab of the login request, store the token using:
2. For subsequent requests, include the token in the headers
Scheduling Automated API Tests
You can use Postman’s Newman to run tests in a CI/CD pipeline or schedule them regularly. Newman is a command-line tool that allows you to execute Postman collections in non-GUI mode.
For example:
Conclusion
Postman is a powerful tool for both manual and automated API testing. By using its automation features like scripting, collections, and environment variables, you can quickly scale your testing efforts. This article demonstrated how to automate a login page API, covering key areas like setting up requests, writing tests, and handling tokens dynamically.
Comments