0
0
Cypresstesting~8 mins

cy.siblings() and cy.closest() in Cypress - Framework Patterns

Choose your learning style9 modes available
Framework Mode - cy.siblings() and cy.closest()
Folder Structure
cypress/
├── e2e/                  # Test specs
│   ├── example.cy.js      # Example test file
│   └── siblingClosest.cy.js  # Tests for cy.siblings() and cy.closest()
├── fixtures/             # Test data files (JSON, etc.)
│   └── example.json
├── support/              # Custom commands and reusable utilities
│   ├── commands.js       # Custom Cypress commands
│   └── e2e.js            # Support file loaded before tests
cypress.config.js         # Cypress configuration file
Test Framework Layers
  • Test Specs (cypress/e2e/): Contains test files that use cy.siblings() and cy.closest() to find related elements in the DOM.
  • Support Layer (cypress/support/): Holds reusable commands and utilities to simplify test code and improve readability.
  • Fixtures (cypress/fixtures/): Stores static test data to keep tests clean and data-driven.
  • Configuration (cypress.config.js): Defines environment settings, base URLs, and browser options.
Configuration Patterns

Use cypress.config.js to manage environment variables and browser settings:

import { defineConfig } from 'cypress'

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

Access environment variables in tests with Cypress.env('username'). This keeps sensitive data out of test code.

Test Reporting and CI/CD Integration
  • Use built-in Cypress test runner reports for quick feedback.
  • Integrate with Mocha reporters or plugins like mochawesome for detailed HTML and JSON reports.
  • Configure CI pipelines (GitHub Actions, Jenkins, GitLab CI) to run Cypress tests on code pushes and pull requests.
  • Fail the build if tests using cy.siblings() or cy.closest() fail, ensuring UI relationships remain stable.
Best Practices
  • Use semantic selectors: Prefer data attributes like data-cy for locating elements to avoid brittle tests.
  • Limit DOM traversal: Use cy.closest() to find the nearest ancestor and cy.siblings() to find peer elements, keeping selectors clear and efficient.
  • Chain commands carefully: Always return Cypress commands to maintain the command queue and avoid flaky tests.
  • Write readable tests: Use descriptive test names and comments to explain why you use cy.siblings() or cy.closest().
  • Use explicit assertions: Assert on the elements found by these commands to verify correct UI behavior.
Self Check

Where in this folder structure would you add a new test file that verifies the behavior of cy.closest() on a navigation menu?

Answer: Add the new test file inside the cypress/e2e/ folder, for example navigationClosest.cy.js.

Key Result
Organize Cypress tests with clear folder layers, use cy.siblings() and cy.closest() for precise DOM traversal, and configure environment settings for maintainable, reliable UI tests.