0
0
Postmantesting~8 mins

Using extracted data in next request in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Using extracted data in next request
Folder Structure
PostmanCollection/
├── collections/
│   └── api-requests.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts.js
│   └── test-scripts.js
├── data/
│   └── test-data.json
└── README.md
Test Framework Layers
  • Collections: Group of API requests organized logically (e.g., user APIs, product APIs).
  • Environment Files: Store variables like base URLs, tokens, and credentials for different environments.
  • Pre-request Scripts: JavaScript code that runs before a request to set or modify variables.
  • Test Scripts: JavaScript code that runs after a request to validate response and extract data.
  • Data Files: External JSON or CSV files used for data-driven testing.
Configuration Patterns
  • Environment Variables: Use environment files to store base URLs, API keys, and tokens. Switch environments to run tests against dev, staging, or prod.
  • Global Variables: For data shared across collections or requests, like auth tokens.
  • Extract and Store Data: Use test scripts to extract data from a response and save it as environment or global variables for use in next requests.
  • Pre-request Scripts: Use these to dynamically set variables before sending requests, such as inserting extracted tokens.
Test Reporting and CI/CD Integration
  • Postman Test Results: View pass/fail status and detailed logs in the Postman Test Results tab after running collections.
  • Newman CLI: Run Postman collections from command line with Newman. Generate reports in formats like HTML, JSON, or JUnit.
  • CI/CD Integration: Integrate Newman runs into pipelines (Jenkins, GitHub Actions, GitLab CI) to automate API testing on code changes.
  • Reporting Tools: Use generated reports to track test coverage, failures, and trends over time.
Best Practices
  • Use Environment Variables: Avoid hardcoding values. Use environment variables for URLs, tokens, and credentials.
  • Extract Data Cleanly: Extract only needed data from responses and store with clear variable names.
  • Chain Requests: Use extracted variables in subsequent requests to simulate real user flows.
  • Keep Scripts Simple: Write clear and small pre-request and test scripts for maintainability.
  • Version Control Collections: Store Postman collections and environment files in version control for collaboration and history.
Self Check

Where in this folder structure would you add a script to extract a token from a login response and use it in the next API request?

Key Result
Use environment variables to extract data from one request and reuse it in the next request for chained API testing.