0
0
PostmanHow-ToBeginner ยท 4 min read

How to Set Execution Order in Postman Collections

In Postman, you set the execution order by arranging requests inside folders or the collection in the desired sequence. You can also control flow using pm.setNextRequest() in test scripts to jump to specific requests dynamically.
๐Ÿ“

Syntax

Postman runs requests in the order they appear inside a collection or folder. To control execution dynamically, use the pm.setNextRequest(requestName) function inside the Tests tab of a request.

  • requestName: The exact name of the next request to run.
  • If pm.setNextRequest(null) is called, the run stops.
javascript
pm.setNextRequest('Request 2');
๐Ÿ’ป

Example

This example shows how to run three requests in order, but skip the third request based on a condition.

javascript
// In Request 1 Tests tab
const runNext = true;
if (runNext) {
    pm.setNextRequest('Request 2');
} else {
    pm.setNextRequest(null); // stops the run
}

// In Request 2 Tests tab
pm.setNextRequest('Request 3');

// In Request 3 Tests tab
pm.setNextRequest(null); // ends the run
Output
Request 1 runs โ†’ Request 2 runs โ†’ Request 3 runs (if runNext is true) or stops after Request 1 (if false)
โš ๏ธ

Common Pitfalls

  • Not naming requests exactly as used in pm.setNextRequest() causes errors.
  • Calling pm.setNextRequest() multiple times in one test script can cause unpredictable behavior.
  • Forgetting to call pm.setNextRequest(null) at the end can cause infinite loops.
  • Relying only on folder order without scripts limits dynamic control.
javascript
// Wrong usage example
pm.setNextRequest('request 2'); // wrong case or typo

// Correct usage example
pm.setNextRequest('Request 2');
๐Ÿ“Š

Quick Reference

FeatureDescription
Collection/Folder OrderRequests run in the order they appear.
pm.setNextRequest()Dynamically jump to a specific request by name.
pm.setNextRequest(null)Stop the collection run immediately.
Request NamingMust match exactly in pm.setNextRequest() calls.
โœ…

Key Takeaways

Arrange requests in folders or collections to set a basic execution order.
Use pm.setNextRequest('RequestName') in test scripts for dynamic control.
Always match request names exactly when using pm.setNextRequest().
Call pm.setNextRequest(null) to stop the run and avoid infinite loops.
Folder order controls static flow; scripts enable conditional execution.