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

Cypress › Retry-ability, waiting, 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

TypeExamplesWhat it does
Queriesget, find, contains, eq, itsReads the DOM, retried
Assertionsshould, andTriggers the retries
Actionsclick, type, checkWaits, runs once
Othersvisit, request, wait, thenOwn 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.

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.

More articles in this chapter