Principle:Cypress io Cypress DOM Assertion and Interaction
| Knowledge Sources | |
|---|---|
| Domains | Testing, Browser_Automation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A retry-based mechanism for querying DOM elements, performing user actions, and asserting on state that automatically retries until conditions are met or a timeout is reached.
Description
DOM assertion and interaction is the core testing primitive in Cypress. Unlike traditional test frameworks where assertions fail immediately, Cypress queries and assertions automatically retry. When cy.get('.btn') does not find an element, it retries the query until the element appears or the timeout expires. When cy.should('be.visible') fails, it retries the preceding query and re-evaluates the assertion.
Actions like click() and type() include actionability checks: they wait for the element to be visible, not disabled, not covered, and not animating before performing the action. This eliminates the flakiness common in browser automation.
Usage
Use this principle in every component and E2E test when interacting with rendered DOM elements. The retry-based approach applies to all cy.get, cy.contains, cy.should, and action commands.
Theoretical Basis
Retry Algorithm:
1. Execute query/assertion
2. If passes → proceed to next command
3. If fails and timeout not reached → wait requestAnimationFrame → retry from step 1
4. If fails and timeout reached → fail test with last error
Actionability Checks (before click/type):
- Element is visible (not display:none, visibility:hidden, etc.)
- Element is not disabled
- Element is not covered by another element
- Element is not animating (position stable between frames)
- Element scrolled into view