QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Retry-ability, waiting, timeouts

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

SituationWhat Cypress does
get + shouldrepeats lookup and assertion up to 4 s
click, typewaits for actionability, one action
.thenruns once, no retry
cy.wait(ms)a fixed pause, no condition
wait with an aliaswaits for request and response
Figure 1. Whatever is not in this table (visit, request, task) has its own timeouts.

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 .then that 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.

In an interview. Junior: Cypress waits for elements by itself: get and should repeat for up to 4 seconds and click waits until the element is available. A fixed cy.wait is bad because it is a guessed pause. Middle/Senior: I wait for a condition: an assertion on a state or cy.wait on an alias from an intercept registered before the action. I change a timeout in a targeted way, and since Cypress 12 I remember that the whole query chain restarts and .then is not retried.

More articles in this chapter