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
cy.getByRole('button', { name: 'Save changes' })Find an element by ARIA role and accessible name (added in 16.2.0)
cy.get('nav').getByRole('link', { name: 'Docs' })Chain getByRole off a container to search only inside it
cy.contains('Submit')Find an element by its text; fine when the text is part of what you test
cy.get('.list').find('li').eq(2)Query inside a previous subject and take the third match
cy.get('a').as('links')Alias a query so later commands can re-run it with cy.get('@links')
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
cy.get('@new').should('have.class', 'active')Assert on an alias; the query re-runs until the class appears or time runs out
.should(($div) => { expect(parseFloat($div.text())).to.be.gte(1) })Retry a custom check written as a callback, instead of using .then()
.should('contain', 'Header').and('be.visible')Chain several assertions on one subject; each must pass in order
cy.get('.el', { timeout: 0 })Disable retrying for one query
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
cy.wait('@getUsers').its('response.statusCode').should('eq', 200)Wait for the aliased request and assert on its response
cy.intercept('GET', '/api/users', { fixture: 'users.json' })Stub the response with a file from cypress/fixtures
cy.intercept('GET', '/api/users', { statusCode: 500, body: {} , delay: 500 })Fake a slow server error to test your error state
cy.intercept('POST', '/users', (req) => { req.reply({ statusCode: 201 }) })Reply from a handler without hitting the server
req.continue((res) => { res.body.isAdmin = true })Let the request reach the server, then change the response
cy.intercept({ method: 'GET', pathname: '/api/users', query: { page: '1' } })Match by path and query object, avoiding ? glob issues
cy.fixture('users.json').then((users) => { ... })Load a fixture file; JSON is parsed for you
cy.fixture('users.json', 'utf8')Load the raw text without parsing
cy.env(['apiUrl', 'apiKey']).then(({ apiUrl, apiKey }) => { ... })Read secret values asynchronously in Cypress 16; replaces Cypress.env()
Cypress.expose('apiVersion')Read a public, non-secret value from the expose block in config
defineConfig({ e2e: { baseUrl: 'http://localhost:1234' } })Set the base URL so cy.visit('/') works and Cypress errors if the server is down
defaultCommandTimeout: 4000How long queries and assertions retry, in ms
pageLoadTimeout: 60000How long cy.visit waits for the page load event
viewportWidth: 1000, viewportHeight: 660Default viewport size in pixels
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}'Default e2e spec location; --spec only runs files that also match it
retries: { runMode: 2, openMode: 0 }Retry failed tests in cypress run only; both default to 0
video: falseVideo recording is off by default; screenshotOnRunFailure defaults to true
npx cypress open --e2e --browser chromeOpen the interactive runner for e2e tests in Chrome
npx cypress runRun all specs headless, the normal CI command
npx cypress run --spec "cypress/e2e/login.cy.js"Run one spec file
npx cypress run --headed --browser firefoxRun in a visible Firefox window
npx cypress run --config pageLoadTimeout=100000Override config values for one run
npx cypress run --record --parallel --group e2eRecord to Cypress Cloud and split specs across CI machines; key from CYPRESS_RECORD_KEY
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 usingbtnlater 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()andcy.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()aftercy.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 toCypress.expose();Cypress.env()is gone in 16.0.
Related cheat sheets
Sources
- https://docs.cypress.io/app/core-concepts/best-practices
- https://docs.cypress.io/app/core-concepts/retry-ability
- https://docs.cypress.io/api/commands/intercept
- https://docs.cypress.io/api/commands/fixture
- https://docs.cypress.io/api/commands/getbyrole
- https://docs.cypress.io/api/cypress-api/env
- https://docs.cypress.io/app/references/configuration
- https://docs.cypress.io/app/references/command-line
- https://docs.cypress.io/app/references/changelog
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.