Bird
Raised Fist0
Postmantesting~8 mins

Run order and flow control in Postman - Framework Patterns

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Framework Mode - Run order and flow control
Folder Structure
postman-project/
├── collections/
│   ├── UserManagement.postman_collection.json
│   ├── ProductAPI.postman_collection.json
│   └── OrderAPI.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts/
│   │   └── authSetup.js
│   ├── test-scripts/
│   │   └── responseValidation.js
│   └── flow-control/
│       └── runOrderControl.js
├── reports/
│   └── test-run-report.html
├── postman.config.json
└── README.md
    
Test Framework Layers
  • Collections: Group of API requests organized by feature or module.
  • Environments: Variables for different deployment stages (dev, staging, prod).
  • Pre-request Scripts: Code that runs before each request to set up data or authentication.
  • Test Scripts: Code that runs after each request to validate responses and assert conditions.
  • Flow Control Scripts: Scripts to control run order and conditional execution using Postman features like postman.setNextRequest().
  • Reports: Generated test run reports for analysis.
  • Configuration: Central config file for environment selection and global settings.
Configuration Patterns
  • Environment Files: Store variables like base URLs, tokens, and credentials per environment.
  • Global Variables: Use Postman globals for values shared across collections.
  • Collection Variables: Define variables scoped to a collection for modularity.
  • Run Order Control: Use postman.setNextRequest(requestName) in test scripts to control which request runs next, enabling conditional flows.
  • Data Files: Use CSV or JSON files with Newman to run data-driven tests.
  • Config File: postman.config.json to define default environment and run settings for automation.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line with options to generate HTML, JSON, or JUnit reports.
  • Reporters: Use built-in reporters like html, json, or third-party reporters for detailed test run insights.
  • CI/CD Integration: Integrate Newman runs into pipelines (Jenkins, GitHub Actions, GitLab CI) to automate API testing on code changes.
  • Fail Fast: Use run order control to stop execution on critical failures by skipping subsequent requests.
  • Artifacts: Store test reports as build artifacts for review and traceability.
Best Practices for Run Order and Flow Control in Postman
  1. Use postman.setNextRequest() to control request flow: This allows conditional execution and dynamic run order based on test results.
  2. Keep collections modular: Organize requests logically so flow control scripts remain clear and maintainable.
  3. Use environment and collection variables: Avoid hardcoding values to enable easy switching between environments and test data.
  4. Fail fast on critical errors: Stop the run early if a key request fails to save time and resources.
  5. Document flow logic: Comment scripts to explain why certain requests run conditionally for easier team understanding.
Self Check

Where in this folder structure would you add a new script to conditionally skip a request based on a previous response?

Key Result
Use postman.setNextRequest() in test scripts to control run order and enable conditional flow in Postman collections.

Practice

(1/5)
1. In Postman, what does pm.setNextRequest() do in a collection run?
easy
A. It sets which request runs next in the collection.
B. It stops the entire collection run immediately.
C. It restarts the current request.
D. It logs the response of the current request.

Solution

  1. Step 1: Understand the purpose of pm.setNextRequest()

    This function controls the flow by specifying the next request to run in the collection.
  2. Step 2: Compare other options

    Stopping the run is done by pm.setNextRequest(null), not this function. Restarting or logging are unrelated.
  3. Final Answer:

    It sets which request runs next in the collection. -> Option A
  4. Quick Check:

    Flow control = set next request [OK]
Hint: Remember: setNextRequest controls next request flow [OK]
Common Mistakes:
  • Confusing pm.setNextRequest() with stopping the run
  • Thinking it restarts the current request
  • Assuming it logs data instead of controlling flow
2. Which of the following is the correct syntax to stop running any further requests in a Postman collection?
easy
A. pm.abortRun()
B. pm.setNextRequest('stop')
C. pm.stopCollection()
D. pm.setNextRequest(null)

Solution

  1. Step 1: Identify the method to stop collection run

    Postman uses pm.setNextRequest(null) to stop running further requests.
  2. Step 2: Verify other options

    Other options like pm.stopCollection() or pm.abortRun() do not exist in Postman scripting.
  3. Final Answer:

    pm.setNextRequest(null) -> Option D
  4. Quick Check:

    Stop run = setNextRequest(null) [OK]
Hint: Use null in setNextRequest to stop run [OK]
Common Mistakes:
  • Using a string like 'stop' instead of null
  • Assuming non-existent functions stop the run
  • Confusing stopping with skipping requests
3. Consider this Postman test script inside a request named 'Request A':
if (pm.response.code === 200) {
  pm.setNextRequest('Request B');
} else {
  pm.setNextRequest(null);
}

What happens if the response code is 404?
medium
A. The collection run continues to 'Request B'.
B. An error is thrown and the run fails.
C. The collection run stops after 'Request A'.
D. The collection run restarts from the first request.

Solution

  1. Step 1: Analyze the condition for response code 404

    Since 404 is not 200, the else block runs pm.setNextRequest(null).
  2. Step 2: Understand effect of pm.setNextRequest(null)

    This stops the collection run immediately after the current request.
  3. Final Answer:

    The collection run stops after 'Request A'. -> Option C
  4. Quick Check:

    404 triggers stop = setNextRequest(null) [OK]
Hint: If condition false, setNextRequest(null) stops run [OK]
Common Mistakes:
  • Assuming it continues to next request regardless
  • Thinking it restarts the collection
  • Believing an error is thrown automatically
4. You wrote this test script in Postman:
pm.setNextRequest('Request C');
pm.setNextRequest(null);

What is the effect on the collection run flow?
medium
A. The run jumps to 'Request C' and then stops.
B. The run stops immediately; 'Request C' is skipped.
C. The run ignores both commands and continues normally.
D. The run loops infinitely between requests.

Solution

  1. Step 1: Understand order of pm.setNextRequest calls

    Only the last pm.setNextRequest() call takes effect in a single script execution.
  2. Step 2: Analyze the last call pm.setNextRequest(null)

    This stops the collection run immediately, ignoring previous setNextRequest calls.
  3. Final Answer:

    The run stops immediately; 'Request C' is skipped. -> Option B
  4. Quick Check:

    Last setNextRequest call wins = null stops run [OK]
Hint: Last setNextRequest call controls flow [OK]
Common Mistakes:
  • Thinking both calls run in sequence
  • Assuming first call overrides last
  • Believing it causes infinite loops
5. You want to run a Postman collection where:
- If 'Login' request succeeds (status 200), run 'GetData'.
- If 'Login' fails, stop the run.
- After 'GetData', always run 'Logout'.

Which sequence of pm.setNextRequest() calls in the 'Login' and 'GetData' test scripts achieves this flow?
hard
A. In 'Login': pm.setNextRequest('GetData') if 200 else pm.setNextRequest(null);
In 'GetData': pm.setNextRequest('Logout')
B. In 'Login': pm.setNextRequest('Logout') always;
In 'GetData': pm.setNextRequest(null)
C. In 'Login': pm.setNextRequest(null) always;
In 'GetData': pm.setNextRequest('Logout')
D. In 'Login': pm.setNextRequest('GetData') always;
In 'GetData': pm.setNextRequest(null)

Solution

  1. Step 1: Control flow after 'Login'

    If status is 200, next request should be 'GetData'; else stop run with pm.setNextRequest(null).
  2. Step 2: Control flow after 'GetData'

    Always run 'Logout' next, so pm.setNextRequest('Logout') is set in 'GetData' tests.
  3. Final Answer:

    In 'Login': pm.setNextRequest('GetData') if 200 else pm.setNextRequest(null); In 'GetData': pm.setNextRequest('Logout') -> Option A
  4. Quick Check:

    Conditional jump + final logout = In 'Login': pm.setNextRequest('GetData') if 200 else pm.setNextRequest(null);
    In 'GetData': pm.setNextRequest('Logout') [OK]
Hint: Use conditional setNextRequest in Login, fixed next in GetData [OK]
Common Mistakes:
  • Setting wrong next request after Login
  • Not stopping run on Login failure
  • Skipping Logout after GetData