Bird
Raised Fist0
Postmantesting~10 mins

Looping in flows in Postman - Test Execution Trace

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using loops in Postman test flows?
easy
A. To create new API endpoints
B. To store environment variables permanently
C. To change the Postman interface theme
D. To repeat test steps automatically for multiple inputs

Solution

  1. Step 1: Understand the role of loops in testing

    Loops allow repeating actions multiple times without manual repetition.
  2. Step 2: Identify the correct purpose in Postman context

    In Postman, loops help run tests on many inputs automatically.
  3. Final Answer:

    To repeat test steps automatically for multiple inputs -> Option D
  4. Quick Check:

    Loops = Repeat steps [OK]
Hint: Loops repeat actions to save time [OK]
Common Mistakes:
  • Thinking loops create new APIs
  • Confusing loops with UI settings
  • Assuming loops store variables permanently
2. Which of the following is the correct syntax to start a for loop in Postman test script?
easy
A. for i = 0; i < 5; i++ { }
B. for (let i = 0; i < 5; i++) { }
C. loop (i = 0; i < 5; i++) { }
D. foreach i in 5 { }

Solution

  1. Step 1: Recall JavaScript for loop syntax

    Postman scripts use JavaScript, so the loop must follow JS syntax.
  2. Step 2: Identify the correct syntax

    Correct JS for loop uses: for (let i = 0; i < 5; i++) { }
  3. Final Answer:

    for (let i = 0; i < 5; i++) { } -> Option B
  4. Quick Check:

    JS for loop = for (init; condition; increment) [OK]
Hint: JS loops use parentheses and braces [OK]
Common Mistakes:
  • Omitting parentheses around loop conditions
  • Using 'loop' or 'foreach' which are invalid in JS
  • Missing 'let' or 'var' declaration
3. What will be the output in Postman console after running this script?
let sum = 0;
for (let i = 1; i <= 3; i++) {
  sum += i;
}
console.log(sum);
medium
A. 6
B. 123
C. 3
D. 0

Solution

  1. Step 1: Trace the loop iterations

    i goes from 1 to 3, sum adds i each time: 0+1=1, 1+2=3, 3+3=6.
  2. Step 2: Confirm final sum value

    After loop ends, sum is 6, which is printed to console.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum 1+2+3 = 6 [OK]
Hint: Add numbers step-by-step in loop [OK]
Common Mistakes:
  • Concatenating numbers as strings instead of adding
  • Stopping loop early or late
  • Printing initial sum without update
4. Identify the error in this Postman test script loop:
for (let i = 0; i < 5; i++)
  console.log(i);
}
medium
A. Loop condition should be i <= 5
B. Incorrect variable declaration for i
C. Missing opening brace '{' after for loop
D. console.log cannot be used inside loops

Solution

  1. Step 1: Check loop syntax

    The for loop lacks an opening brace '{' after the loop declaration.
  2. Step 2: Confirm braces balance

    There is a closing brace '}' without a matching opening brace, causing syntax error.
  3. Final Answer:

    Missing opening brace '{' after for loop -> Option C
  4. Quick Check:

    Braces must match in loops [OK]
Hint: Always open and close braces in loops [OK]
Common Mistakes:
  • Ignoring missing braces
  • Changing loop condition unnecessarily
  • Thinking console.log is invalid inside loops
5. You want to run a test in Postman that sends requests for each user ID in an array [101, 102, 103]. Which looping method is best to use inside the pm.sendRequest callback to handle each ID sequentially?
hard
A. Use a recursive function to send requests one after another
B. Use a forEach loop without waiting for requests to finish
C. Use a simple for loop with asynchronous calls inside
D. Use a while loop that never ends

Solution

  1. Step 1: Understand asynchronous request handling

    pm.sendRequest is async; simple loops run all requests at once without waiting.
  2. Step 2: Choose method to run requests sequentially

    Recursive function can wait for each request to finish before next starts, ensuring order.
  3. Final Answer:

    Use a recursive function to send requests one after another -> Option A
  4. Quick Check:

    Sequential async calls = recursion [OK]
Hint: Use recursion for sequential async loops [OK]
Common Mistakes:
  • Using forEach which runs all requests in parallel
  • Using infinite while loops causing hang
  • Assuming simple for loops wait for async calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard