0
0
Cypresstesting~8 mins

cy.url() assertions in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - cy.url() assertions
Folder Structure
  cypress/
  ├── e2e/
  │   ├── login.cy.js          # Test files using cy.url() assertions
  │   └── dashboard.cy.js
  ├── fixtures/                # Test data files
  │   └── users.json
  ├── support/
  │   ├── commands.js          # Custom commands
  │   └── e2e.js               # Global support file
  └── cypress.config.js        # Cypress configuration
  
Test Framework Layers
  • Test Files (e2e/): Contains test scripts where cy.url() assertions verify the current page URL after actions.
  • Support Layer (support/): Holds reusable commands and setup code. Custom commands can wrap cy.url() checks for reuse.
  • Fixtures: Store test data like URLs or expected paths to keep tests clean and data-driven.
  • Configuration (cypress.config.js): Defines baseUrl, environment variables, and browser settings for consistent test runs.
Configuration Patterns
  • Base URL: Set baseUrl in cypress.config.js to avoid hardcoding full URLs in tests.
  • Environment Variables: Use env in config or CLI to switch URLs or credentials per environment (dev, staging, prod).
  • Browser Selection: Configure browsers in cypress.config.js or via CLI to test URL behavior across browsers.
  • Timeouts: Adjust command timeouts to wait for URL changes before asserting.
Test Reporting and CI/CD Integration
  • Use built-in Cypress reporters or plugins like mochawesome for detailed test reports including cy.url() assertion results.
  • Integrate Cypress tests into CI/CD pipelines (GitHub Actions, Jenkins) to run URL assertion tests on every code push.
  • Failing cy.url() assertions cause test failures, clearly showing URL mismatches in reports.
  • Use screenshots and video recordings on failure to capture the URL state visually.
Best Practices
  • Always use cy.url().should() with clear, specific assertions like include, eq, or match for readability.
  • Use baseUrl and relative paths to keep tests flexible and environment-independent.
  • Wrap common URL assertions in custom commands for reuse and cleaner tests.
  • Wait for navigation or page load before asserting URL to avoid flaky tests.
  • Keep URL assertions focused on important parts (path, query params) rather than full URLs when possible.
Self Check

Where in this folder structure would you add a new custom command that checks if the current URL contains a specific path segment?

Key Result
Organize Cypress tests with clear folder layers, config for environments, reusable commands, and robust reporting to validate URLs using cy.url() assertions.