0
0
Postmantesting~8 mins

Extracting data from responses in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Extracting data from responses
Folder Structure
PostmanCollectionProject/
├── collections/
│   └── MyAPICollection.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts/
│   │   └── setupVariables.js
│   └── test-scripts/
│       └── extractData.js
├── reports/
│   └── test-report.html
└── README.md
Test Framework Layers
  • Collections: Group of API requests organized by feature or endpoint.
  • Environments: Store variables like base URLs, tokens for different setups (dev, staging, prod).
  • Pre-request Scripts: JavaScript code run before requests to set or modify variables.
  • Test Scripts: JavaScript code run after responses to validate and extract data.
  • Utilities: Shared scripts or functions for common tasks like parsing JSON or setting variables.
  • Reports: Generated test run reports showing pass/fail and extracted data.
Configuration Patterns
  • Environment Variables: Use environment files to store URLs, tokens, and credentials. Switch environments to run tests in different setups.
  • Global Variables: For data shared across collections or requests.
  • Collection Variables: Variables scoped to a collection for modularity.
  • Data Files: Use CSV or JSON files to run data-driven tests with different input values.
  • Scripts: Use pre-request scripts to set dynamic variables before requests. Use test scripts to extract data from responses and save to variables for later use.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line with newman. Supports JSON, HTML, and JUnit reports.
  • Report Generation: Generate detailed HTML or JSON reports showing which tests passed or failed and extracted data values.
  • CI/CD Integration: Integrate Newman runs into pipelines (Jenkins, GitHub Actions, GitLab CI) to automate API testing on code changes.
  • Environment Selection: Pass environment files to Newman to test different setups automatically.
  • Failure Alerts: Configure pipeline to notify team on test failures for quick feedback.
Best Practices
  • Use Clear Variable Names: Name extracted variables clearly to avoid confusion.
  • Extract Only Needed Data: Extract minimal data needed for next requests or validations to keep tests simple.
  • Validate Before Extracting: Always check response status and structure before extracting data to avoid errors.
  • Use Environment Variables: Store extracted data in environment variables to share between requests.
  • Keep Scripts Modular: Use separate script files or functions for extraction logic to reuse and maintain easily.
Self Check

Where in this framework structure would you add a new script to extract a user ID from a login response and save it for later requests?

Key Result
Organize Postman tests with collections, environment variables, and test scripts to extract and reuse response data efficiently.