0
0
Postmantesting~15 mins

Run order and flow control in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify run order and flow control in Postman collection
Preconditions (4)
Step 1: Run the collection
Step 2: Observe that Request1 runs first
Step 3: Observe that Request2 runs second and sets the 'runNext' variable
Step 4: Observe that Request3 runs only if 'runNext' is 'true'
Step 5: If 'runNext' is 'false', Request3 should be skipped
✅ Expected Result: Request1 and Request2 always run in order. Request3 runs only if the 'runNext' variable is set to 'true' by Request2, demonstrating run order and flow control.
Automation Requirements - Postman Collection Runner with pm scripting
Assertions Needed:
Verify Request1 runs before Request2
Verify Request2 sets environment variable 'runNext'
Verify Request3 runs only if 'runNext' is 'true'
Verify Request3 is skipped if 'runNext' is 'false'
Best Practices:
Use pm.environment.set and pm.environment.get for variable control
Use pm.test for assertions
Use postman.setNextRequest() to control flow
Keep scripts simple and readable
Automated Solution
Postman
/* Request1 Tests */
pm.test('Request1 runs', function () {
    pm.expect(true).to.be.true;
});

/* Request2 Tests and flow control */
// In Tests tab of Request2
pm.test('Request2 runs and sets runNext', function () {
    pm.environment.set('runNext', 'true'); // Change to 'false' to test skipping
    pm.expect(pm.environment.get('runNext')).to.be.oneOf(['true', 'false']);
});

// Control flow
if (pm.environment.get('runNext') === 'true') {
    postman.setNextRequest('Request3');
} else {
    postman.setNextRequest(null); // Stops collection run
}

/* Request3 Tests */
pm.test('Request3 runs only if runNext is true', function () {
    pm.expect(pm.environment.get('runNext')).to.eql('true');
});

This script uses Postman test scripts to control the run order and flow.

In Request2, we set an environment variable runNext to 'true' or 'false'.

Then, using postman.setNextRequest(), we decide if Request3 should run.

If runNext is 'true', Request3 runs next; otherwise, the collection stops.

Assertions verify that each request runs in order and that flow control works as expected.

Common Mistakes - 4 Pitfalls
Not using postman.setNextRequest() to control flow
Setting environment variables in the wrong script tab
Using global variables instead of environment variables
Not clearing or resetting variables before collection run
Bonus Challenge

Now add data-driven testing by running the collection with three different values of 'runNext' ('true', 'false', 'true') and verify Request3 runs accordingly.

Show Hint