0
0
Postmantesting~15 mins

Dynamic URL building in Postman - Build an Automation Script

Choose your learning style9 modes available
Build and test dynamic URL with query parameters in Postman
Preconditions (2)
Step 1: Open Postman and create a new GET request
Step 2: Set the base URL to https://api.example.com/items
Step 3: Add query parameters dynamically: category=books, sort=price_asc, limit=10
Step 4: Send the request
Step 5: Verify the request URL includes all query parameters correctly
Step 6: Verify the response status code is 200
Step 7: Verify the response body contains items filtered by category 'books'
✅ Expected Result: The request URL is https://api.example.com/items?category=books&sort=price_asc&limit=10, the response status code is 200, and the response body contains items filtered by category 'books'
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Verify request URL contains all query parameters
Verify response status code is 200
Verify response body contains expected filtered data
Best Practices:
Use Postman environment variables for dynamic parts
Use pm.request.url.query to verify query parameters
Use pm.response.to.have.status to check status code
Use JSON parsing and assertions for response body
Keep test scripts clear and maintainable
Automated Solution
Postman
pm.test('Request URL contains all query parameters', () => {
    const url = pm.request.url.toString();
    pm.expect(url).to.include('category=books');
    pm.expect(url).to.include('sort=price_asc');
    pm.expect(url).to.include('limit=10');
});

pm.test('Response status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body contains items filtered by category books', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.be.an('object');
    pm.expect(jsonData.items).to.be.an('array').that.is.not.empty;
    jsonData.items.forEach(item => {
        pm.expect(item.category).to.eql('books');
    });
});

The first test checks that the request URL includes all the expected query parameters by converting the request URL to a string and verifying each parameter is present.

The second test asserts that the response status code is 200, indicating a successful request.

The third test parses the JSON response body, ensures it contains an array of items, and verifies each item's category is 'books' to confirm filtering worked.

Using Postman built-in pm API ensures clear and maintainable test scripts.

Common Mistakes - 4 Pitfalls
Hardcoding full URL instead of using environment variables
Not verifying all query parameters in the request URL
Assuming response body structure without validation
Using pm.response.text() instead of pm.response.json() for JSON responses
Bonus Challenge

Now add data-driven testing with 3 different sets of query parameters (e.g., category=books, category=electronics, category=clothing) and verify the response accordingly

Show Hint