0
0
Postmantesting~15 mins

Query parameters in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API response with query parameters
Preconditions (2)
Step 1: Open Postman and create a new GET request
Step 2: Enter the URL https://api.example.com/items
Step 3: Add query parameter 'category' with value 'books'
Step 4: Add query parameter 'limit' with value '5'
Step 5: Send the request
Step 6: Observe the response status code and body
✅ Expected Result: Response status code is 200 OK and response body contains a list of 5 items all belonging to category 'books'
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Verify response status code is 200
Verify response body is an array
Verify each item in response has category 'books'
Verify response array length is 5
Best Practices:
Use pm.* API for assertions
Check response status before parsing body
Use descriptive assertion messages
Keep test scripts simple and readable
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body is an array of 5 items', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.be.an('array', 'Response is not an array');
    pm.expect(jsonData.length).to.eql(5, 'Response array length is not 5');
});

pm.test('All items have category "books"', () => {
    const jsonData = pm.response.json();
    jsonData.forEach(item => {
        pm.expect(item.category).to.eql('books', `Item category is not 'books': ${JSON.stringify(item)}`);
    });
});

The first test checks that the response status code is 200, confirming the request was successful.

The second test parses the response body as JSON and verifies it is an array with exactly 5 items, matching the 'limit' query parameter.

The third test loops through each item in the response array and asserts that the 'category' field equals 'books', matching the 'category' query parameter.

Using descriptive messages helps identify which assertion failed if the test does not pass.

Common Mistakes - 3 Pitfalls
Not verifying the response status code before parsing the body
Hardcoding expected values inside test without using query parameter values
Not checking the response body type before asserting properties
Bonus Challenge

Now add data-driven testing with 3 different sets of query parameters: category=books & limit=5, category=electronics & limit=3, category=clothing & limit=4

Show Hint