Skip to content

Menu

Cypress Cheat Sheet: Commands, Retries, cy.intercept and Config

Checked against Cypress Cypress 16.4.0 (released 25 Nov 2026), docs only on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers writing Cypress end-to-end tests who want the commands and defaults in one place. It follows docs.cypress.io for Cypress 16.4.0, released on 25 November 2026. Cypress isn't installed in our environment, so every row is marked untested and comes straight from the official docs.

When to use what

The docs rank selectors clearly: tag names and styling classes never, ids and name attributes sparingly, text and role queries depending on the case, and a dedicated data-cy attribute always. Since 16.2 you also have cy.getByRole(), which is a good default for buttons and headings and nudges you towards accessible markup. The Cypress command builder can draft a chain if you can't remember the syntax.

The thing to understand before anything else is retrying. Queries such as cy.get() and .find() and assertions such as .should() retry for four seconds by default; actions such as .click() and .type() run once. So put the assertion right after the query that might be slow, and don't chain new queries after an action. For network, stub with cy.intercept() and wait on its alias rather than sleeping. If you're choosing between Cypress and Playwright, the test runner comparison lays out the trade-offs, and Cypress Cloud AI is the hosted side with recording and test analytics.

cy.get('[data-cy="submit"]')

Select by a dedicated test attribute, the option the docs mark as always fine

Try it: cypress-command-builderFrom the docs, not run by us
cy.getByRole('button', { name: 'Save changes' })

Find an element by ARIA role and accessible name (added in 16.2.0)

From the docs, not run by us
cy.get('nav').getByRole('link', { name: 'Docs' })

Chain getByRole off a container to search only inside it

From the docs, not run by us
cy.contains('Submit')

Find an element by its text; fine when the text is part of what you test

From the docs, not run by us
cy.get('.list').find('li').eq(2)

Query inside a previous subject and take the third match

From the docs, not run by us
cy.get('a').as('links')

Alias a query so later commands can re-run it with cy.get('@links')

From the docs, not run by us

Queries and assertions retry up to defaultCommandTimeout (4000 ms). Actions and .then() run once.

cy.get('[data-testid="mobile-nav"]', { timeout: 10000 }).should('be.visible')

Allow one slow query more time instead of raising the global timeout

From the docs, not run by us
cy.get('@new').should('have.class', 'active')

Assert on an alias; the query re-runs until the class appears or time runs out

From the docs, not run by us
.should(($div) => { expect(parseFloat($div.text())).to.be.gte(1) })

Retry a custom check written as a callback, instead of using .then()

From the docs, not run by us
.should('contain', 'Header').and('be.visible')

Chain several assertions on one subject; each must pass in order

From the docs, not run by us
cy.get('.el', { timeout: 0 })

Disable retrying for one query

From the docs, not run by us

Define a route before the request is sent: call cy.intercept() before cy.visit() or the click that triggers it. cy.request() calls are never intercepted.

cy.intercept('GET', '/api/users').as('getUsers')

Spy on a request and name it so you can wait for it

Try it: http-status-testerFrom the docs, not run by us
cy.wait('@getUsers').its('response.statusCode').should('eq', 200)

Wait for the aliased request and assert on its response

From the docs, not run by us
cy.intercept('GET', '/api/users', { fixture: 'users.json' })

Stub the response with a file from cypress/fixtures

From the docs, not run by us
cy.intercept('GET', '/api/users', { statusCode: 500, body: {} , delay: 500 })

Fake a slow server error to test your error state

From the docs, not run by us
cy.intercept('POST', '/users', (req) => { req.reply({ statusCode: 201 }) })

Reply from a handler without hitting the server

From the docs, not run by us
req.continue((res) => { res.body.isAdmin = true })

Let the request reach the server, then change the response

From the docs, not run by us
cy.intercept({ method: 'GET', pathname: '/api/users', query: { page: '1' } })

Match by path and query object, avoiding ? glob issues

From the docs, not run by us
cy.fixture('users.json').then((users) => { ... })

Load a fixture file; JSON is parsed for you

From the docs, not run by us
cy.fixture('users.json', 'utf8')

Load the raw text without parsing

From the docs, not run by us
cy.env(['apiUrl', 'apiKey']).then(({ apiUrl, apiKey }) => { ... })

Read secret values asynchronously in Cypress 16; replaces Cypress.env()

From the docs, not run by us
Cypress.expose('apiVersion')

Read a public, non-secret value from the expose block in config

From the docs, not run by us
defineConfig({ e2e: { baseUrl: 'http://localhost:1234' } })

Set the base URL so cy.visit('/') works and Cypress errors if the server is down

From the docs, not run by us
defaultCommandTimeout: 4000

How long queries and assertions retry, in ms

From the docs, not run by us
pageLoadTimeout: 60000

How long cy.visit waits for the page load event

From the docs, not run by us
viewportWidth: 1000, viewportHeight: 660

Default viewport size in pixels

From the docs, not run by us
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}'

Default e2e spec location; --spec only runs files that also match it

From the docs, not run by us
retries: { runMode: 2, openMode: 0 }

Retry failed tests in cypress run only; both default to 0

From the docs, not run by us
video: false

Video recording is off by default; screenshotOnRunFailure defaults to true

From the docs, not run by us
npx cypress open --e2e --browser chrome

Open the interactive runner for e2e tests in Chrome

From the docs, not run by us
npx cypress run

Run all specs headless, the normal CI command

From the docs, not run by us
npx cypress run --spec "cypress/e2e/login.cy.js"

Run one spec file

From the docs, not run by us
npx cypress run --headed --browser firefox

Run in a visible Firefox window

From the docs, not run by us
npx cypress run --config pageLoadTimeout=100000

Override config values for one run

From the docs, not run by us
npx cypress run --record --parallel --group e2e

Record to Cypress Cloud and split specs across CI machines; key from CYPRESS_RECORD_KEY

From the docs, not run by us

Worked example: Stub an API, wait for it and assert the empty state

Say the users page should show "No users yet" when the API returns an empty list. Stub the call, visit, then wait on the alias:

// cypress/e2e/users.cy.js
it('shows the empty state', () => {
  cy.intercept('GET', '/api/users', { statusCode: 200, body: [] }).as('getUsers')
  cy.visit('/users')
  cy.wait('@getUsers').its('response.statusCode').should('eq', 200)
  cy.getByRole('heading', { name: 'No users yet' }).should('be.visible')
})

The order matters. The intercept is registered before cy.visit, so the page's first request hits the stub; register it after the visit and the real API answers instead. cy.wait('@getUsers') holds the test until that request has happened, which replaces a guessed cy.wait(2000). The final query retries for up to four seconds, so a slow render still passes. We couldn't run Cypress here, so we can't show the runner's log for this spec; in the Command Log you should see the route listed, the XHR matched to getUsers, and the assertion turn green.

Common mistakes

  • Writing const btn = cy.get('button') and using btn later as if it were an element.

    Cypress commands are queued, not returned; use an alias with .as() or a .then() callback.

  • Adding cy.wait(3000) to wait for data to load.

    Alias the request with cy.intercept().as() and cy.wait('@alias'), or assert on the element you expect.

  • Cleaning up test data in afterEach, so a refreshed or aborted run leaves dirty state.

    Reset state in beforeEach, as the docs recommend, so every test starts clean whatever happened before.

  • Calling cy.intercept() after cy.visit() and wondering why the stub never fires.

    Register intercepts before the action that sends the request.

  • Upgrading to Cypress 16 with Cypress.env('apiKey') calls still in the specs.

    Move secrets to cy.env([...]) and public values to Cypress.expose(); Cypress.env() is gone in 16.0.