Test Overview
This test checks if a Postman collection correctly loops through multiple user IDs and fetches their details one by one. It verifies that the API response for each user contains the expected user ID.
This test checks if a Postman collection correctly loops through multiple user IDs and fetches their details one by one. It verifies that the API response for each user contains the expected user ID.
const userIds = [1, 2, 3]; let currentIndex = pm.environment.get('currentIndex') || 0; const userId = userIds[currentIndex]; pm.sendRequest(`https://jsonplaceholder.typicode.com/users/${userId}`, function (err, res) { pm.test("Loop through user IDs and verify response", function () { pm.expect(err).to.be.null; pm.expect(res).to.have.property('code', 200); const jsonData = res.json(); pm.expect(jsonData.id).to.eql(userId); }); currentIndex++; if (currentIndex < userIds.length) { pm.environment.set('currentIndex', currentIndex); postman.setNextRequest(pm.info.requestName); } else { pm.environment.unset('currentIndex'); postman.setNextRequest(null); } });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and reads currentIndex from environment (initially 0) | Environment variable 'currentIndex' is 0 or undefined | - | PASS |
| 2 | Sends GET request to https://jsonplaceholder.typicode.com/users/1 | Browser/Postman sends request for user ID 1 | Response status code is 200 | PASS |
| 3 | Checks response JSON contains id = 1 | Response JSON has user data with id 1 | jsonData.id equals 1 | PASS |
| 4 | Increments currentIndex to 1 and sets environment variable | Environment variable 'currentIndex' is now 1 | - | PASS |
| 5 | Sets next request to the same request to loop | Postman will run the same request again | - | PASS |
| 6 | Sends GET request to https://jsonplaceholder.typicode.com/users/2 | Browser/Postman sends request for user ID 2 | Response status code is 200 | PASS |
| 7 | Checks response JSON contains id = 2 | Response JSON has user data with id 2 | jsonData.id equals 2 | PASS |
| 8 | Increments currentIndex to 2 and sets environment variable | Environment variable 'currentIndex' is now 2 | - | PASS |
| 9 | Sets next request to the same request to loop | Postman will run the same request again | - | PASS |
| 10 | Sends GET request to https://jsonplaceholder.typicode.com/users/3 | Browser/Postman sends request for user ID 3 | Response status code is 200 | PASS |
| 11 | Checks response JSON contains id = 3 | Response JSON has user data with id 3 | jsonData.id equals 3 | PASS |
| 12 | Increments currentIndex to 3 and unsets environment variable | Environment variable 'currentIndex' is removed | - | PASS |
| 13 | Sets next request to null to stop looping | Postman stops running the request | - | PASS |