Looping in flows helps you repeat actions automatically. This saves time and avoids mistakes when testing many similar cases.
Looping in flows in Postman
pm.iterationData.each(function(data) {
// Your test code here using data
});pm.iterationData holds data from a data file (like CSV or JSON) used in collection runs.
You write your test steps inside the function to run them for each data item.
pm.iterationData.each(function(item) {
console.log(item.username);
});for (let i = 0; i < 5; i++) { console.log(`Loop number: ${i}`); }
This script loops 3 times, simulating sending 3 requests. Each loop prints a message and runs a simple test to check the loop count.
for (let i = 1; i <= 3; i++) { console.log(`Sending request number ${i}`); pm.test(`Test for request ${i}`, function () { pm.expect(i).to.be.below(4); }); }
Postman scripts run in JavaScript, so you can use any JavaScript loop like for, while, or forEach.
Use loops carefully to avoid infinite loops that freeze your tests.
Looping with pm.iterationData works only when running collections with data files.
Looping repeats test steps automatically to save time.
Use JavaScript loops inside Postman scripts for flexible control.
Loops help test many inputs or repeat actions easily.
Practice
Solution
Step 1: Understand the role of loops in testing
Loops allow repeating actions multiple times without manual repetition.Step 2: Identify the correct purpose in Postman context
In Postman, loops help run tests on many inputs automatically.Final Answer:
To repeat test steps automatically for multiple inputs -> Option DQuick Check:
Loops = Repeat steps [OK]
- Thinking loops create new APIs
- Confusing loops with UI settings
- Assuming loops store variables permanently
Solution
Step 1: Recall JavaScript for loop syntax
Postman scripts use JavaScript, so the loop must follow JS syntax.Step 2: Identify the correct syntax
Correct JS for loop uses: for (let i = 0; i < 5; i++) { }Final Answer:
for (let i = 0; i < 5; i++) { } -> Option BQuick Check:
JS for loop = for (init; condition; increment) [OK]
- Omitting parentheses around loop conditions
- Using 'loop' or 'foreach' which are invalid in JS
- Missing 'let' or 'var' declaration
let sum = 0;
for (let i = 1; i <= 3; i++) {
sum += i;
}
console.log(sum);Solution
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.Step 2: Confirm final sum value
After loop ends, sum is 6, which is printed to console.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Concatenating numbers as strings instead of adding
- Stopping loop early or late
- Printing initial sum without update
for (let i = 0; i < 5; i++) console.log(i); }
Solution
Step 1: Check loop syntax
The for loop lacks an opening brace '{' after the loop declaration.Step 2: Confirm braces balance
There is a closing brace '}' without a matching opening brace, causing syntax error.Final Answer:
Missing opening brace '{' after for loop -> Option CQuick Check:
Braces must match in loops [OK]
- Ignoring missing braces
- Changing loop condition unnecessarily
- Thinking console.log is invalid inside loops
[101, 102, 103]. Which looping method is best to use inside the pm.sendRequest callback to handle each ID sequentially?Solution
Step 1: Understand asynchronous request handling
pm.sendRequest is async; simple loops run all requests at once without waiting.Step 2: Choose method to run requests sequentially
Recursive function can wait for each request to finish before next starts, ensuring order.Final Answer:
Use a recursive function to send requests one after another -> Option AQuick Check:
Sequential async calls = recursion [OK]
- Using forEach which runs all requests in parallel
- Using infinite while loops causing hang
- Assuming simple for loops wait for async calls
Ready to prove it?
Pass the test and this pattern moves unknown
