0
0
Cypresstesting~8 mins

Multiple assertions chaining in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Multiple assertions chaining
Folder Structure
cypress/
├── e2e/
│   ├── login.spec.js
│   ├── dashboard.spec.js
│   └── multipleAssertions.spec.js  <-- Test file for multiple assertions chaining
├── support/
│   ├── commands.js  <-- Custom commands for reusable actions
│   └── e2e.js       <-- Global support file
cypress.config.js    <-- Cypress configuration file
Test Framework Layers
  • Test Files (cypress/e2e): Contains test scripts using multiple assertions chaining to verify UI elements in one flow.
  • Support Layer (cypress/support): Holds reusable commands and setup code to keep tests clean and DRY.
  • Configuration (cypress.config.js): Defines environment settings like baseUrl, browser options, and test retries.
Configuration Patterns

Use cypress.config.js to manage environments and browsers:

import { defineConfig } from 'cypress'

export default defineConfig({
  e2e: {
    baseUrl: 'https://example.com',
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
    env: {
      username: 'testuser',
      password: 'password123'
    },
    retries: 1
  }
})

Access credentials in tests via Cypress.env('username'). This keeps sensitive data out of test code.

Test Reporting and CI/CD Integration
  • Use built-in Cypress Dashboard or plugins like mochawesome for detailed HTML reports.
  • Integrate Cypress tests in CI/CD pipelines (GitHub Actions, Jenkins) to run tests on every code push.
  • Configure test retries and screenshots/videos on failure for easier debugging.
Best Practices for Multiple Assertions Chaining
  1. Chain assertions on the same element: Use Cypress chaining to keep tests readable and efficient, e.g., cy.get('button').should('be.visible').and('contain', 'Submit').
  2. Use explicit waits carefully: Prefer Cypress automatic waits but add should assertions to wait for conditions.
  3. Keep assertions focused: Don't chain too many assertions that test unrelated things in one chain.
  4. Use custom commands: For repeated assertion chains, create custom commands to reduce duplication.
  5. Clear error messages: Write assertions that fail clearly to help debugging.
Self Check

Where in this folder structure would you add a new custom command to reuse a common multiple assertions chain for a login button?

Key Result
Organize Cypress tests with clear folder structure, chain assertions for readability, and configure environments for flexible testing.