0
0
Postmantesting~15 mins

Looping in flows in Postman - Build an Automation Script

Choose your learning style9 modes available
Automate looping through multiple API requests in Postman collection runner
Preconditions (3)
Step 1: Open the Postman collection runner
Step 2: Select the collection containing the API request
Step 3: Load a data file (CSV or JSON) with multiple sets of input data
Step 4: Start the collection runner to execute the request for each data row
Step 5: Observe that the request runs once per data row, looping through all inputs
Step 6: Verify the response status code is 200 for each request
Step 7: Verify the response body contains expected data for each input
✅ Expected Result: The collection runner executes the API request once per data row, looping through all inputs. Each response returns status 200 and expected data.
Automation Requirements - Postman test scripts with Collection Runner
Assertions Needed:
Verify response status code is 200
Verify response body contains expected data based on input
Verify the loop runs for all data rows
Best Practices:
Use pm.test() for assertions
Use data-driven testing with external data files
Use pm.iterationData to access current data row
Keep test scripts simple and readable
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const expectedValue = pm.iterationData.get('expectedValue');

pm.test('Response body contains expected value', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedValue);
});

This Postman test script runs after each API request in the collection runner.

First, it checks that the response status code is 200 using pm.response.to.have.status(200).

Then, it retrieves the expected value for the current iteration from the data file using pm.iterationData.get('expectedValue').

Finally, it asserts that the response JSON's value field equals the expected value.

This script runs once per data row, enabling looping through inputs in the collection runner.

Common Mistakes - 3 Pitfalls
Hardcoding expected values inside the test script instead of using data files
Not checking the response status code before validating response body
Using console.log for validation instead of pm.test assertions
Bonus Challenge

Now add data-driven testing with 3 different inputs using a JSON data file

Show Hint