0
0
Cypresstesting~8 mins

Asserting request properties in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Asserting request properties
Folder Structure
  cypress/
  ├── e2e/                  # Test specs
  │   └── api/              # API related tests
  │       └── requestAssertions.cy.js
  ├── fixtures/             # Test data files (JSON, etc.)
  ├── support/              # Custom commands and utilities
  │   ├── commands.js       # Custom Cypress commands
  │   └── index.js          # Support file loaded before tests
  ├── plugins/              # Cypress plugins (if needed)
  └── cypress.config.js     # Cypress configuration file
  
Test Framework Layers
  • Test Specs (e2e/): Contains test files that define the test cases asserting request properties using cy.intercept() and cy.wait().
  • Support Layer (support/): Holds reusable custom commands and utilities to simplify request assertions and setup.
  • Fixtures (fixtures/): Stores static test data used in requests or responses for consistent testing.
  • Configuration (cypress.config.js): Defines environment variables, base URLs, and browser settings.
  • Plugins (plugins/): Optional layer for extending Cypress capabilities if needed.
Configuration Patterns
  • Environment Variables: Use cypress.config.js to define env properties for API base URLs, credentials, and feature flags.
  • Multiple Environments: Use --env CLI option or separate config files for dev, staging, and production URLs.
  • Browser Selection: Configure default browser in cypress.config.js or via CLI for cross-browser testing.
  • Credentials Management: Store sensitive data in environment variables or CI secrets, avoid hardcoding in tests.
  • Request Interception Setup: Define intercept aliases in test setup or support commands for reuse.
Test Reporting and CI/CD Integration
  • Built-in Cypress Reporter: Shows test pass/fail status and detailed logs in the Test Runner UI.
  • CI Integration: Run Cypress tests in CI pipelines (GitHub Actions, Jenkins, GitLab CI) using cypress run command.
  • Generate Reports: Use plugins like mochawesome to create HTML and JSON reports for test results.
  • Fail Fast: Configure CI to stop on first failure or continue to gather full test suite results.
  • Artifacts: Save screenshots and videos of failed tests for debugging.
Best Practices
  • Use cy.intercept() with Aliases: Assign aliases to intercepted requests to wait and assert on them clearly.
  • Assert on Request Properties: Check method, URL, headers, body, and response status to ensure correct API behavior.
  • Keep Tests Independent: Avoid relying on previous test state; intercept and assert requests within each test.
  • Use Fixtures for Test Data: Load request or response data from fixtures for consistency and maintainability.
  • Custom Commands: Create reusable commands for common request assertions to reduce duplication.
Self Check

Where in this folder structure would you add a new custom command to assert common request headers?

Key Result
Use cy.intercept() with aliases in Cypress tests to assert request properties clearly and reliably.