0
0
Postmantesting~5 mins

Looping in flows in Postman

Choose your learning style9 modes available
Introduction

Looping in flows helps you repeat actions automatically. This saves time and avoids mistakes when testing many similar cases.

You want to test an API with different input values one after another.
You need to send multiple requests with slight changes in each.
You want to check how your system handles a list of items.
You want to repeat a test step until a condition is met.
You want to automate repetitive tasks in your test collection.
Syntax
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.

Examples
This prints the username from each data row during the test run.
Postman
pm.iterationData.each(function(item) {
    console.log(item.username);
});
This simple loop runs 5 times and prints the loop count each time.
Postman
for (let i = 0; i < 5; i++) {
    console.log(`Loop number: ${i}`);
}
Sample Program

This script loops 3 times, simulating sending 3 requests. Each loop prints a message and runs a simple test to check the loop count.

Postman
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);
    });
}
OutputSuccess
Important Notes

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.

Summary

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.