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 |
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');
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
.thenas 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.