0
0
Postmantesting~8 mins

Setting variables in scripts in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Setting variables in scripts
Folder Structure for Postman Test Automation
PostmanProject/
├── collections/
│   └── MyAPICollection.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts/
│   │   └── setVariables.js
│   └── test-scripts/
│       └── validateResponse.js
├── globals.json
└── README.md
Test Framework Layers in Postman
  • Collections: Group of API requests organized logically.
  • Environments: Store variables for different deployment stages (dev, staging, prod).
  • Scripts:
    • Pre-request scripts: Run before requests to set or update variables.
    • Test scripts: Run after requests to validate responses and set variables.
  • Globals: Variables accessible across all collections and environments.
  • Utilities: Reusable JavaScript functions for common tasks (can be included via external files or snippets).
Configuration Patterns for Variables in Postman
  • Environment Variables: Define variables per environment (e.g., base URL, tokens). Switch environments to run tests in different contexts.
  • Global Variables: Use for values shared across all environments and collections.
  • Setting Variables in Scripts:
    pm.environment.set("variableName", "value");
    pm.globals.set("variableName", "value");
    pm.variables.set("variableName", "value");
    Use pm.variables for temporary variables valid only during the request execution.
  • Clearing Variables: Remove variables when no longer needed:
    pm.environment.unset("variableName");
  • Secure Variables: Store sensitive data in environment variables, not in collections.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line for automation.
  • Reports: Generate HTML, JSON, or JUnit reports using Newman reporters.
  • CI/CD Integration: Integrate Newman runs in pipelines (Jenkins, GitHub Actions, GitLab CI) to run tests on code changes.
  • Variable Management: Use environment files and secure storage in CI to manage variables safely.
Best Practices for Setting Variables in Postman Scripts
  1. Use Environment Variables for Configurable Data: Keep URLs, tokens, and credentials in environment variables to easily switch contexts.
  2. Set Variables Explicitly in Scripts: Use pm.environment.set() or pm.variables.set() to update variables clearly and predictably.
  3. Clear Variables When Done: Remove temporary variables to avoid stale data affecting other tests.
  4. Keep Sensitive Data Secure: Never hardcode secrets in scripts; use environment variables with restricted access.
  5. Use Globals Sparingly: Prefer environment variables over globals to avoid unintended side effects.
Self Check

Where in this folder structure would you add a new script to set a variable before sending a request?

Key Result
Organize Postman tests with collections, environments, and scripts to manage variables clearly and securely.