0
0
Cypresstesting~8 mins

Programmatic login (cy.request) in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Programmatic login (cy.request)
Folder Structure
  cypress/
  ├── e2e/
  │   ├── login.cy.js          # Tests using programmatic login
  │   └── otherFeature.cy.js
  ├── support/
  │   ├── commands.js          # Custom commands including programmatic login
  │   └── e2e.js               # Support file loaded before tests
  ├── fixtures/
  │   └── user.json            # Test user data (username, password)
  cypress.config.js            # Cypress configuration file
  
Test Framework Layers
  • Test Layer: Test files in cypress/e2e/ that call programmatic login before UI actions.
  • Support Layer: commands.js defines cy.login() using cy.request() to authenticate without UI.
  • Fixtures Layer: Stores user credentials and test data in JSON files for reuse.
  • Configuration Layer: cypress.config.js manages baseUrl, environment variables, and test settings.
Configuration Patterns
  • Use cypress.config.js to set baseUrl for different environments (dev, staging, prod).
  • Store sensitive data like usernames and passwords in cypress/fixtures/user.json or environment variables.
  • Access credentials in tests or commands via cy.fixture() or Cypress.env().
  • Use cy.request() in commands.js to send login API calls programmatically.
Test Reporting and CI/CD Integration
  • Use Cypress built-in reporter for test results in the terminal.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins) to run tests on code changes.
  • Generate HTML reports using plugins like mochawesome for detailed test reports.
  • Fail fast on login failures to avoid false positives in UI tests.
Best Practices
  • Use programmatic login with cy.request() to speed up tests by skipping UI login steps.
  • Keep login logic in commands.js to reuse across tests and keep tests clean.
  • Store credentials securely and avoid hardcoding sensitive data in test files.
  • Use fixtures for test data to separate data from test logic.
  • Ensure tests verify login success by checking for expected UI elements after login.
Self Check

Where in this folder structure would you add a new API command for logging out programmatically?

Key Result
Use cy.request() in support/commands.js for fast, reusable programmatic login in Cypress tests.