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
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.
Related lessons: “Queries, actions and assertions: what is the difference?”, “Why is cy.wait(3000) an antipattern and what to use instead?”, “What timeouts does Cypress have and how do you override them?”.
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.
Queries, actions and assertions: what is the difference?
A query reads state and is repeated, an action changes application state and runs once, and an assertion drives the retries. The criterion is whether the command changes state. cy.visit, cy.request, cy.wait and .then have no retry loop, only their own timeouts. So after an action a new chain starts from cy.get, because the element may have been re-rendered.
Cypress · Waiting
Which command in a chain is retried and which runs only once?
The chain cy.get(...).contains(...).click().should(...) has four commands, and each belongs to a different group. That decides what Cypress re-runs on a failure and what it does not. This lesson gives a simple rule for classifying a command and shows how to build chains with it.
How it works and why. The split rests on one question: does the command change app state. If not, it can be run as often as needed, so it is a query and Cypress repeats it until the assertion passes. If yes, repeating is dangerous and it is an action. Assertions belong to neither: they drive the retries. All other commands (visit, request, wait, then) have no retry loop, only their own timeouts.
Four groups of commands
Type
Examples
What it does
Queries
get, find, contains, eq, its
Reads the DOM, retried
Assertions
should, and
Triggers the retries
Actions
click, type, check
Waits, runs once
Others
visit, request, wait, then
Own timeouts
Figure 1. One question decides the group: does the command change app state.
There are implicit assertions too. cy.get has a built-in check that the element exists, and before running, actions implicitly require the element to be visible, not disabled and not covered. So cy.get(...).click() without an explicit should waits as well. Since Cypress 12 you can also write queries yourself with Cypress.Commands.addQuery: such a command is retried in the same way.
Example: reading a chain.
cy.get('[data-cy=row]') // query
.contains('Paid') // query
.should('be.visible') // assertion
.click(); // action: once, after actionability// after the action: a new chain
cy.get('[data-cy=status]').should('have.text', 'Closed');
If should('be.visible') fails, Cypress repeats get and contains, not the click. After the click there is no query to restart, so the next check starts with a new cy.get: the app could have re-rendered the element.
Limits and alternatives. Negative assertions deserve care: should('not.exist') passes as soon as the element is missing, including when it has not yet had time to appear.
// passes at once if the spinner has not appeared yet
cy.get('[data-cy=spinner]').should('not.exist');
// safer: wait for it to appear first, then to disappear
cy.get('[data-cy=spinner]').should('be.visible');
cy.get('[data-cy=spinner]').should('not.exist');
In Playwright:
There is no such split: a locator does nothing by itself, an action waits for the element to be ready and expect(locator) retries. Only an assertion over an already obtained value is not retried, for example expect(await locator.isVisible()).toBe(true).
Common mistakes.
Treating .then as part of the retry loop: it interrupts it.
Writing an assertion after an action and wondering why the element went stale.
Starting with should('not.exist') for a spinner that has not appeared: the check passes vacuously.
Check yourself
In cy.get('a').click().should('have.class', 'on') the click re-renders the node. What went wrong and what do you do?
Answer
The assertion after the action checks the same element the click yielded and does not look it up again. Split it: cy.get('a').click(); cy.get('a').should('have.class', 'on').
Which group does cy.wait('@alias') belong to and is it retried?
Answer
To the other commands: it is neither a query nor an action, it has no retry loop, only the requestTimeout and responseTimeout timeouts.
Related lessons: “What is retry-ability and what exactly is repeated?”, “Why is cy.wait(3000) an antipattern and what to use instead?”, “Why can't you write await cy.get() in Cypress?”.
In an interview.Junior: Queries (get, find, contains) read the DOM and are retried, assertions (should, and) trigger those retries, actions (click, type) wait until the element is available and run once. Middle/Senior: The criterion is simple: does the command change state. Hence the rules: I put an assertion after a query, not after an action, start a new chain after an action, do not check in .then what can still change, and back negative assertions (not.exist) with an earlier check that the element appeared.
Why is cy.wait(3000) an antipattern and what to use instead?
A fixed pause like cy.wait(3000) is a guessed number: it knows nothing about application state, so it either slows the test or comes too short. Cypress already waits by itself, so the pause is replaced with a condition: an assertion on UI state, or cy.wait on an alias of a request caught by cy.intercept, registered before the action. Timers are controlled with cy.clock() and cy.tick().
Cypress · Waiting
A test with cy.wait(3000) is green but fails once in ten runs in CI. What do you replace it with?
Someone added cy.wait(3000) after saving because "it did not make it otherwise". Locally it is fine. In CI, where the machine is slower, 3 seconds is sometimes not enough, and when it is enough the test still drags in every run. This lesson explains why a fixed pause loses in both cases and what replaces it.
How it works and why.cy.wait(ms) just stops the test for the given time and knows nothing about the app. Cypress already waits by itself: queries with assertions are retried up to defaultCommandTimeout and actions wait for actionability. So the pause is replaced by waiting for a condition, and the test waits exactly as long as needed, but no longer than the timeout.
A pause or a condition
cy.wait(3000)
A guessed pause
The response came in 0.5 s: 2.5 s lost in every run. It came in 4 s: the test failed. Slow and flaky at once.
Waiting for a state
should / cy.wait(@alias)
The test moves on as soon as the condition holds and waits for it, not for a fixed time.
Figure 1. A pause waits a fixed time, a condition waits for the needed state.
Example: two replacements. There are two reliable ways: a state in the UI or a specific network request.
// before
cy.get('[data-cy=save]').click();
cy.wait(3000);
// 1. wait for a state in the UI
cy.get('[data-cy=save]').click();
cy.get('[data-cy=toast]').should('contain', 'Saved');
// 2. wait for a specific request (intercept BEFORE the action)
cy.intercept('POST', '/api/profile').as('save');
cy.get('[data-cy=save]').click();
cy.wait('@save').its('response.statusCode').should('eq', 200);
The second way requires cy.intercept to be registered before the action: otherwise the request slips past. cy.wait('@save') waits for the request to go out (requestTimeout, 5 s by default) and for the response (responseTimeout, 30 s), then yields the interception you can check. If there are several requests, each next cy.wait('@save') takes the next interception in order.
Limits and alternatives. UI or request: a UI assertion answers "what does the user see", a request answers "did the call go out". The first is better by default, the second is needed when the UI shows no result or you must check the request body. Real time is rarely needed. For debounce, timers and animations, control the time through cy.clock() and cy.tick().
// a 500 ms debounce: control the time instead of waiting for it
cy.clock(); // before cy.visit: the page's timers are faked
cy.visit('/search');
cy.get('[data-cy=search]').type('qa');
cy.tick(500);
cy.get('[data-cy=hint]').should('be.visible');
In Playwright:
The same rule: page.waitForTimeout() is an antipattern. Use a web-first assertion or page.waitForResponse() instead, which, like an alias in Cypress, is set up before the action.
Common mistakes.
Replacing cy.wait(3000) with cy.wait(5000) "to be safe": a slower test, the same cause.
Registering cy.intercept after the click: the request has gone, and cy.wait times out.
Waiting for an alias and not checking the response: the test is green even when the server returned an error.
Check yourself
Why does cy.wait(3000) make a test both slow and flaky?
Answer
Because 3 seconds is a guess. If the app is faster, time is lost in every run; if slower (CI), the test goes on before the app is ready and fails.
cy.wait('@save') fails with "No request ever occurred" although the request is visible in the network. What is the cause?
Answer
Most likely cy.intercept(...).as('save') was registered after the action that sent the request, or the URL pattern or method does not match. Set the intercept before the click and check the method and path.
Related lessons: “What is retry-ability and what exactly is repeated?”, “What timeouts does Cypress have and how do you override them?”, “What is cy.intercept and where exactly does the interception happen?”.
In an interview.Junior: It is a guessed number: if the app is faster the test waits for nothing, if slower it fails. Instead I wait for a state: an assertion on an element or an intercept alias with cy.wait. Middle/Senior: I choose what to wait for: a UI assertion when what the user sees matters, a request when the call must be checked. I register the intercept before the action. For debounce and timers I use cy.clock and cy.tick, because real time is almost never needed in a test.
What timeouts does Cypress have and how do you override them?
Each Cypress command group has its own timeout, overridden by the timeout option on a command, in it or describe config, or in cypress.config. defaultCommandTimeout (4 s) applies to queries and assertions, requestTimeout (5 s) and responseTimeout (30 s) to the network, pageLoadTimeout (60 s) to cy.visit, taskTimeout (60 s) to cy.task. Only the one that fired is raised, because a global increase hides slowness.
Cypress · Waiting
A report takes 12 seconds to generate and get fails on the fourth. Raise the timeout for everything?
One slow screen breaks a test and the temptation is strong: raise defaultCommandTimeout in the config. Then every real failure waits longer and a performance problem hides. This lesson explains which timeouts Cypress has, what they apply to and how to change one without touching the rest.
How it works and why. Cypress has no single "general" timeout: each group of commands has its own, because they wait for different things. The defaults come from the configuration docs.
Which timer on which command
Figure 1. Each timer covers its own kind of waiting, so fix the one that actually fired.
Timeout
Default
Applies to
defaultCommandTimeout
4 s
queries, assertions, actionability
requestTimeout
5 s
until the request goes out (cy.wait)
responseTimeout
30 s
the response (cy.request, cy.wait)
pageLoadTimeout
60 s
cy.visit, cy.go, cy.reload
taskTimeout
60 s
cy.task
The error message names the timeout that fired (for example "Timed out retrying after 4000ms"), so read it first instead of guessing. For cy.wait('@alias') two timers apply in sequence: first waiting for the request to start, then for the response. During cy.request, responseTimeout applies.
Reading the error: the number that names the timer
AssertionError: Timed out retrying after 14000ms: Expected to find element: 2[data-cy=report], but never found it.
What: 4000 ms is the default defaultCommandTimeout. The number shows which timer fired.
Where: the command that gave up was looking for this selector. Give more time to that command, not to the whole suite.
What to do
If the screen is really slow, add the option to this command only: cy.get('[data-cy=report]', { timeout: 15000 }).
Figure 2. The real error text: the numbers lead to the explanations.
Example: where to override. From narrower to wider: a command, a block of tests, the config.
// 1. one command
cy.get('[data-cy=report]', { timeout: 15000 }).should('be.visible');
// 2. one describe or itdescribe('slow reports', { defaultCommandTimeout: 10000 }, () => {
it('generates a PDF', () => {
cy.get('[data-cy=pdf]').should('exist');
});
});
// 3. waiting for a request
cy.wait('@report', { responseTimeout: 60000 });
// 4. globally (the last resort): cypress.config.ts// e2e: { defaultCommandTimeout: 6000 }
A targeted value is honest: it says that this particular action is slow. A global one hides slow places and lengthens every failure. If a screen is always slow, that is a reason to ask developers about performance, not about a timeout.
Limits and alternatives. A command timeout does not limit the whole test: by default Cypress imposes no single limit on a test, so a test can last the sum of all waits. Hangs are caught at the CI job level. A larger timeout does not cure flakiness, it only gives more time.
In Playwright:
There is a whole-test timeout (timeout, 30 s), a separate one for expect (5 s) and optional actionTimeout and navigationTimeout. The key difference: in Playwright the test is bounded as a whole, in Cypress by default it is not.
Common mistakes.
Raising defaultCommandTimeout globally because of one slow screen.
Changing the wrong timeout: a slow API response is handled by responseTimeout, not a get timeout.
Assuming Cypress will cut off a hung test: there is no per-test limit by default.
Check yourself
cy.visit fails after 60 s and cy.get('x') after 4 s. Why different numbers and what do you change if the screen itself is slow?
Answer
They are different timeouts: pageLoadTimeout for page load and defaultCommandTimeout for queries. For a slow element raise the timeout of that command only, not the global one.
Which timeouts bound cy.wait('@alias') and in what order do they apply?
Answer
Two, in sequence: requestTimeout (5 s) waits for the request to go out, responseTimeout (30 s) waits for the response.
Related lessons: “What is retry-ability and what exactly is repeated?”, “Why is cy.wait(3000) an antipattern and what to use instead?”, “A test is flaky on CI. How do you look for the cause in Cypress?”.
In an interview.Junior: The main one is defaultCommandTimeout, 4 seconds for lookups and assertions. For the network there are requestTimeout and responseTimeout, for cy.visit pageLoadTimeout, for cy.task taskTimeout. I override with the timeout option on a command. Middle/Senior: I change only the timeout that fired, and in a targeted way: on a command, in a describe or it config. A global increase hides performance problems. Cypress has no per-test limit by default, so I catch hangs at the CI level.
A test is flaky on CI. How do you look for the cause in Cypress?
A flaky test in CI is investigated in order: evidence, reproduction, cause, not by adding retries. Evidence: the failure screenshot, video (off by default) and the timeout message. Then the spec is run in a loop on CI-like resources and typical causes are checked: cy.wait instead of a state, an action before the API response, an intercept after the action, shared data. A test that passed on a retry is still investigated.
Cypress · Waiting
A test passes locally and fails in CI once in five runs. Where do you start?
A flaky test is worse than a steadily red one: people stop trusting it and real failures hide in the noise. The temptation is simple: add retries and move on. This lesson gives an investigation order that leads to the cause, not to muting the symptom.
How it works and why. Flakiness almost always means a race: the test makes a step before the app is ready or depends on something it does not control. So the investigation goes from facts to a hypothesis, not the other way round.
Investigation order
Figure 1. Retries come last: they do not find the cause, they only allow a rerun to pass.
1. Evidence.cypress run takes a screenshot on failure automatically. Video is off by default (video: false), so turn it on for CI. Cypress Cloud offers Test Replay with the DOM, network and console. Read the error message: it names the timeout and the command the test failed on.
2-3. Reproduction and cause. Run the spec many times in a row on the same resources as CI: a Cypress._.times loop or the burn mode of the @cypress/grep plugin. Then go through the typical causes.
cy.wait(ms) instead of waiting for a state.
An action before the API response arrived, or an intercept after the action.
Shared data between tests and dependence on order.
Animations and re-rendering of an element between a query and an action.
// 1. reproduce: the spec 20 timesCypress._.times(20, (i) => {
it('checkout #' + i, () => {
// the flaky test
});
});
// 2. cause: an intercept after the action
cy.get('[data-cy=pay]').click();
cy.intercept('POST', '/api/pay').as('pay'); // the request has gone
cy.wait('@pay');
// 3. fix: the intercept before the action
cy.intercept('POST', '/api/pay').as('pay');
cy.get('[data-cy=pay]').click();
cy.wait('@pay');
The cause is visible only because the test ran in a loop: a single run could pass. The fix puts the intercept before the click, so the event is no longer lost.
Limits and alternatives.retries: { runMode: 2, openMode: 0 } (both 0 by default) help reach a release and cut noise, but a test that passed on the second attempt is still flaky and needs analysis. Not every failure comes from the test: sometimes it is a real app bug that shows up rarely.
In Playwright:
The main tool is Trace Viewer: trace: 'on-first-retry' gives a timeline with DOM snapshots, network and console without a separate service. Reproducing is --repeat-each=20, and checking contention for data is --workers=1.
Common mistakes.
Turning retries on before investigating: the cause stays and the metric turns green.
Reproducing on a laptop instead of CI resources: the race may not show.
Adding cy.wait "to make it pass": it only moves the problem.
Check yourself
A test in CI passed on the second attempt. Why is it not "green" and what next?
Answer
retries.runMode kicked in: the result depends on a retry, so there is a race or a state dependency. Investigate it as flaky: evidence, reproduction in a loop, cause.
There is no video in CI although you "never turned it off". Why?
Answer
The video option defaults to false. Enable it explicitly in the config, for example video: true for CI.
Related lessons: “What is retry-ability and what exactly is repeated?”, “Why is cy.wait(3000) an antipattern and what to use instead?”, “What timeouts does Cypress have and how do you override them?”.
In an interview.Junior: I look at the failure screenshot and video, read the timeout message, run the spec several times and look for the cause: cy.wait, data, test order. Middle/Senior: Evidence first (video must be turned on, Test Replay if Cloud is available), then reproduction in a loop on CI resources, then the cause: a network race, shared data, an intercept after the action. Retries in runMode help reach a release, but I analyse every test that passed on a retry.
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
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.
Related lessons: “What is retry-ability and what exactly is repeated?”, “Queries, actions and assertions: what is the difference?”, “Why is cy.wait(3000) an antipattern and what to use instead?”.
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.