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

Cypress › Retry-ability, waiting, timeouts

What is retry-ability and what exactly is repeated?

Retry-ability is Cypress repeating the preceding queries and the check itself when an assertion fails, up to a 4 s timeout. Queries (get, find, contains) are repeated together with the assertion, while actions (click, type) run once because a repeat would change the application state. Since Cypress 12 the whole chain of queries restarts. A then callback, visit, request, task and wait are not retried.

Cypress · Waiting

The check in .then() fails but the same check in .should() passes. Why?

An order list loads from an API. A test with .then(($li) => expect($li).to.have.length(3)) goes red because at call time there are still two items. Replacing it with .should('have.length', 3) turns it green. The difference is not syntax but what gets repeated. This lesson covers which Cypress commands retry, where the retry loop breaks and what changed in Cypress 12.

How it works and why. Cypress commands fall into three groups. Queries (get, find, contains, eq, first, filter, its) only read state, so repeating them is safe. Assertions (should, and) look up nothing, but they are what triggers the retries: when an assertion fails, Cypress repeats both the lookup and the check. Actions (click, type, check, select) change the state of the app, so they run once. A repeated click could create an order twice.

The retry loop

1. get + find: look upfrom the DOM every time2. should: checkfailed? pause, back to step 13. Timeout 4 sthen the test fails with the last error
Figure 1. Without an explicit assertion the loop still runs, but it only waits for the built-in condition that the element exists.

The timeout of this loop is defaultCommandTimeout, 4 seconds by default (from the configuration docs). Since Cypress 12 the whole chain of queries is repeated from the start, not only the last command. Before that Cypress could hold an old element that the framework had already re-rendered and fail with a detached-DOM error. Now in cy.get('[data-cy=orders]').find('li').first().should('contain', 'Paid') every attempt runs get, find and first again. Several assertions in a chain must pass in the same attempt.

Example: where the retry disappears. The first source of flakiness is .then.

Not retried

.then(callback)

The callback runs once with whatever was there at call time. The list is still loading: expect failed and nothing retries.

Retried

.should(callback)

The callback runs again together with the preceding queries until it passes or the timeout ends.

// before: .then runs once
cy.get('li').then(($li) => {
  expect($li).to.have.length(3);
});

// after: the query and the assertion retry together
cy.get('li').should('have.length', 3);

// a complex check: the callback inside should
cy.get('li').should(($li) => {
  expect($li).to.have.length(3);
  expect($li.first()).to.contain('Paid');
});

.then breaks the chain: nothing before it is executed again. The second trap is an assertion right after an action. The action ran once and the .should after it checks the same element the click yielded. If the app re-rendered it, nothing looks it up again. The documentation advises splitting such a chain.

// risky: the assertion follows an action
cy.get('[data-cy=tab-stats]').click().should('have.class', 'active');

// safe: a new chain starting with a query
cy.get('[data-cy=tab-stats]').click();
cy.get('[data-cy=tab-stats]').should('have.class', 'active');

Limits and alternatives. cy.visit, cy.request, cy.task, cy.wait and .then are not retried: they have their own timeouts. Before an action Cypress waits for actionability (the element is in the DOM, visible, not disabled, not animating, not covered) and re-runs the query chain, so "once" does not mean "blindly". Do not call a function with side effects through .invoke(): it can be called several times during retries. A value needed later is taken in .then or an alias, but a check on it is no longer retried.

In Playwright:

The mechanism differs: both actions and assertions wait. An action checks actionability and looks the element up by the locator each time, and expect(locator) retries. There is no queries/actions split, and the trap is different: expect(await locator.isVisible()).toBe(true) checks the state once.

Common mistakes.

  • Checking in .then something that is still loading: the callback runs once and the test loses the race.
  • Putting .should right after .click() on an element that re-renders: nothing looks it up again.
  • Expecting a retry from cy.request or cy.task: they run once.
  • Raising the timeout instead of replacing .then with .should.

Check yourself

A test: cy.get('li').then(($li) => expect($li).to.have.length(3)). The list loads for 1 s. What happens and how do you fix it?

Answer

The .then callback runs once at once, sees fewer items and fails with no retry. The fix: cy.get('li').should('have.length', 3), where the query and the assertion retry for up to 4 seconds.

Why is click not retried although Cypress "waits"?

Answer

A repeated click changes app state (for example, creates a second order). So the action runs once, but before it Cypress waits for actionability and re-runs the query chain leading to the element.

In an interview. Junior: Queries (get, find, contains) are retried together with the should after them, up to 4 seconds. Actions like click run once because a repeat could change state twice. Middle/Senior: Since Cypress 12 every attempt re-runs the whole query chain, so the element is looked up again each time. I write checks with should, not then, because then does not retry, and after an action I start a new chain with cy.get. What is not retried at all (visit, request, task, wait) has its own timeouts.

More articles in this chapter