0
0
Postmantesting~10 mins

Looping in flows in Postman - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Postman
Postman
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);
    }
});
Execution Trace - 13 Steps
StepActionSystem StateAssertionResult
1Test starts and reads currentIndex from environment (initially 0)Environment variable 'currentIndex' is 0 or undefined-PASS
2Sends GET request to https://jsonplaceholder.typicode.com/users/1Browser/Postman sends request for user ID 1Response status code is 200PASS
3Checks response JSON contains id = 1Response JSON has user data with id 1jsonData.id equals 1PASS
4Increments currentIndex to 1 and sets environment variableEnvironment variable 'currentIndex' is now 1-PASS
5Sets next request to the same request to loopPostman will run the same request again-PASS
6Sends GET request to https://jsonplaceholder.typicode.com/users/2Browser/Postman sends request for user ID 2Response status code is 200PASS
7Checks response JSON contains id = 2Response JSON has user data with id 2jsonData.id equals 2PASS
8Increments currentIndex to 2 and sets environment variableEnvironment variable 'currentIndex' is now 2-PASS
9Sets next request to the same request to loopPostman will run the same request again-PASS
10Sends GET request to https://jsonplaceholder.typicode.com/users/3Browser/Postman sends request for user ID 3Response status code is 200PASS
11Checks response JSON contains id = 3Response JSON has user data with id 3jsonData.id equals 3PASS
12Increments currentIndex to 3 and unsets environment variableEnvironment variable 'currentIndex' is removed-PASS
13Sets next request to null to stop loopingPostman stops running the request-PASS
Failure Scenario
Failing Condition: API returns a status code other than 200 or response JSON id does not match expected userId
Execution Trace Quiz - 3 Questions
Test your understanding
What causes the test to loop through multiple user IDs?
ASetting 'postman.setNextRequest' to the same request name
BUsing a for loop inside the test script
CCalling pm.sendRequest multiple times synchronously
DSetting environment variable 'currentIndex' to zero
Key Result
Use environment variables and 'postman.setNextRequest' to create loops in Postman tests, allowing repeated requests with different data in a controlled flow.