0
0
Postmantesting~15 mins

Response body inspection in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify response body contains expected user data
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/users/123
Step 2: Inspect the response body
Step 3: Verify the response body contains the user id as 123
Step 4: Verify the response body contains the user name as 'John Doe'
Step 5: Verify the response body contains the user email as 'john.doe@example.com'
✅ Expected Result: The response body should include the correct user id, name, and email as specified
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Response status code is 200
Response body contains user id 123
Response body contains user name 'John Doe'
Response body contains user email 'john.doe@example.com'
Best Practices:
Use pm.response.to.have.status for status code assertion
Parse response body JSON once and reuse
Use strict equality checks for values
Add clear error messages in assertions
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const responseJson = pm.response.json();

pm.test('User ID is 123', () => {
    pm.expect(responseJson.id, 'User ID').to.eql(123);
});

pm.test('User name is John Doe', () => {
    pm.expect(responseJson.name, 'User name').to.eql('John Doe');
});

pm.test('User email is john.doe@example.com', () => {
    pm.expect(responseJson.email, 'User email').to.eql('john.doe@example.com');
});

This script runs inside Postman’s Tests tab after the request is sent.

First, it checks the HTTP status code is 200 to confirm the request succeeded.

Then it parses the response body JSON once and stores it in responseJson for reuse.

Next, it verifies the id, name, and email fields exactly match the expected values using strict equality (to.eql).

Each assertion has a clear label to help identify which check failed if any test does not pass.

Common Mistakes - 4 Pitfalls
Not checking the status code before inspecting the response body
Parsing response body JSON multiple times
Using loose equality (==) instead of strict equality (=== or to.eql)
Not providing descriptive messages in assertions
Bonus Challenge

Now add data-driven testing with 3 different user IDs and verify their respective names and emails

Show Hint