Steps to Automate API Testing with Postman
To automate API testing with Postman using the JSONPlaceholder API(a freely accessible online sample API). let’s walk through how to automate API testing with Postman.
JSONPlacefolder is a mock online REST API for testing and prototyping. This is a great resource for practicing API testing. The base URL of JSONPlacefolder is https://jsonplacefolder.typicode.com.
If you don’t have Postman installed, download and install it.
Step 1: Open Postman application
Step 2: Create a New Collection
- Click on the “Collections” tab in the left sidebar.
- Click “New Collection.”
- Name the collection, e.g., “JSONPlaceholder API Tests.”
Step 3: Create Requests in the Collection
Request: GET Users
- Click on the “JSONPlaceholder API Tests” collection.
- Click “Add Request” within the collection.
- Name the request, e.g., “Get Users.”
- Set the request type to GET.
- Enter the URL: https://jsonplaceholder.typicode.com/users.
- Click “Save.”
Request: POST Create a Post
1. Click “Add Request” again.
2. Name the request, e.g., “Create a Post.”
3. Set the request type to POST.
4. Enter the URL: https://jsonplaceholder.typicode.com/posts.
5. In the “Body” tab, select “raw” and enter a sample JSON payload:
{
“title”: “Post from Postman”,
“body”: “This is a test post created using Postman”,
“userId”: 1
}
6. Click “Save.”
Step 4: Write Test Scripts
Now, let’s add test scripts to verify the responses.
For “Get Users” Request:
Javascript
// Check if the status code is 200 (OK) pm.test( "Status code is 200" , function () { pm.response.to.have.status(200); }); // Check if the response has at least one user pm.test( "Response has users" , function () { // Parse the JSON response const jsonData = pm.response.json(); // Check if the response is an array and has at least one element pm.expect(jsonData).to.be.an( 'array' ).and.to.have.lengthOf.at.least(1); }); |
For “Create a Post” Request:
Javascript
Create a new post pm.test( 'Create a new post' , async () => { const response = await pm.request( 'Create a Post' ); // Check the status code pm.expect(response.code).to.equal(201); // Check the response body pm.expect(response.json()).to.have.property( 'title' ); }); |
1. Click on the “Runner” tab in the top menu.
2. Select the collection “JSONPlaceholder API Tests.”
3. Click “Run JSONPlaceholder API Tests.”
4. Run the collection.
5. Review the tests results.
Install Postman:
1. Open a terminal window.
2. Run the following command:
npm install -g newman
Note:
This will install the Postman CLI globally.
Once Postman CLI is installed, run it from any directory on your machine. To schedule tests to run automatically using the Postman CLI, use the following command:
Running Tests Manually:
newman run <collection-name>
Here the collection is “JSONPlaceholder API Tests.postman_collection.json” so we have entered the command.
newman run “JSONPlaceholder API Tests.postman_collection.json”
Scheduling Tests to Run Automatically:
newman run <collection-name> –schedule <schedule-expression>
schedule-expression is a cron expression th sets the time the test should execute. The cron expression you provided, 0 0 * * *, will run the test every day at midnight.
newman run “JSONPlaceholder API Tests.postman_collection.json” –schedule “0 0 * * *”
1. Open the Postman app.
2. Enter a name for the monitor and select the collection you want to monitor.
3. Configuring a collection-based monitor.
4. Run the tests.
Automating API Testing with Postman
Testing the functionality, dependability, and performance of an API is known as API testing. API testing can be done automatically or manually. The technique of automating the execution of API tests with tools is known as automated API testing. This can save time and effort, as well as ensure that APIs are tested more thoroughly.