0
0
Cypresstesting~8 mins

cy.session() for session caching in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - cy.session() for session caching
Folder Structure of a Cypress Test Project Using cy.session()
  cypress/
  ├── e2e/                  # Test specs
  │   ├── login.cy.js       # Tests using cy.session() for login caching
  │   └── otherTests.cy.js  # Other test files
  ├── fixtures/             # Test data files (JSON, etc.)
  │   └── user.json         # User credentials for login
  ├── support/              # Support files
  │   ├── commands.js       # Custom commands including login with cy.session()
  │   └── e2e.js            # Global setup for tests
  └── cypress.config.js     # Cypress configuration file
  
Test Framework Layers
  • Test Specs (e2e/): Contains test files that use cy.session() to cache login sessions and speed up tests.
  • Fixtures (fixtures/): Holds test data like user credentials used in session setup.
  • Support (support/): Includes custom commands (e.g., login command using cy.session()) and global test setup.
  • Configuration (cypress.config.js): Defines environment variables, base URL, and browser settings.
Configuration Patterns

Use cypress.config.js to manage environments and credentials securely:

  • Define env variables for usernames and passwords to avoid hardcoding.
  • Set baseUrl for different environments (dev, staging, prod).
  • Use cy.session() in custom commands to cache login sessions per user.
  // cypress.config.js
  export default {
    e2e: {
      baseUrl: 'https://example.com',
      env: {
        username: 'testuser',
        password: 'password123'
      }
    }
  }
  

Example custom command using cy.session():

  // cypress/support/commands.js
  Cypress.Commands.add('login', () => {
    cy.session('user-session', () => {
      cy.visit('/login')
      cy.get('#username').type(Cypress.env('username'))
      cy.get('#password').type(Cypress.env('password'))
      cy.get('button[type=submit]').click()
      cy.url().should('not.include', '/login')
    })
  })
  
Test Reporting and CI/CD Integration
  • Use Cypress built-in reporters (spec, mocha) for clear test results.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins) to run tests on each commit.
  • Cache sessions with cy.session() to reduce test run time and improve CI efficiency.
  • Generate HTML reports using plugins like mochawesome for easy sharing.
Best Practices for Using cy.session()
  • Cache only stable sessions: Use cy.session() for login or setup steps that rarely change.
  • Clear sessions when needed: Use cy.session.clearAllSavedSessions() in before hooks if tests require fresh state.
  • Use descriptive session keys: Name sessions clearly to avoid confusion (e.g., 'admin-user-session').
  • Keep session setup fast and reliable: Avoid flaky steps inside cy.session() callback.
  • Store sensitive data securely: Use environment variables, never hardcode passwords in code.
Self-Check Question

Where in this folder structure would you add a new custom command that uses cy.session() to cache a login session for a new user role?

Key Result
Use cy.session() in Cypress custom commands to cache login sessions and speed up test execution.