Waiting and retry-ability
Cypress waits for elements itself: queries with assertions are retried, and actions run once after the element is checked to be ready. A fixed cy.wait(ms) is bad because it is a guessed pause, so a condition is awaited: an assertion on state or an alias of a cy.intercept request registered before the action. Automation will not wait for an event not reflected in the DOM or a request.
Cypress · Waiting
What does Cypress wait for by itself and what must a test spell out?
This is a digest of the waiting chapter: what Cypress does without your help, where its automation ends and how to wait properly. The details of each point are in the separate lessons of the chapter.
How it works and why. Query commands (cy.get, find, contains) repeat until the assertion passes or defaultCommandTimeout, 4 seconds, runs out. Assertions through .should() repeat together with the preceding queries, and since Cypress 12 the whole chain repeats, not just the last command. Actions (click, type) run once, but first wait for the element to be visible, not covered and not disabled.
Waiting cheat sheet
| Situation | What Cypress does |
|---|---|
| get + should | repeats lookup and assertion up to 4 s |
| click, type | waits for actionability, one action |
| .then | runs once, no retry |
| cy.wait(ms) | a fixed pause, no condition |
| wait with an alias | waits for request and response |
Example: how to wait properly.
// not this: a fixed pause
cy.wait(5000);
// this: a state in the UI
cy.get('[data-cy=report]').should('be.visible');
// this: a specific request
cy.intercept('GET', '/api/report').as('report');
cy.get('[data-cy=run]').click();
cy.wait('@report');
cy.wait(5000) is bad because it is a fixed pause: in a fast environment it needlessly slows the run, in a slow one it may not be enough. A timeout for a specific command is changed with the timeout option, not globally.
Limits and alternatives. The automation does not know your logic: it will not wait for an event reflected neither in the DOM nor in a request. Then make the state visible to the test (an indicator, an attribute) or intercept the request. Playwright has a similar principle, but both actions and assertions wait through a locator.
Common mistakes.
cy.wait(ms)instead of waiting for a condition.- A check in
.thenthat is not retried. - A global timeout increase instead of a targeted one.
Check yourself
Why is cy.wait(5000) worse than should('be.visible')?
Answer
A pause is fixed and knows no state: in a fast environment it slows, in a slow one it is not enough. The assertion waits exactly as long as needed, up to the timeout.
Which commands does Cypress not retry and why?
Answer
Actions (click, type), because they change state, and commands like visit, request, task, wait, then: they have their own timeouts, not a retry loop.