Skip to main content
Quick Reference

Cypress Cheatsheet

Cypress on one page — queries, retries, intercepts, and the traps around the command queue, aliases, and cy.wait.

10 sections 10 tables 14 code snippets

Visit and click

A test selector

cy.visit("/cart")
cy.get("[data-cy=save]").click()

By visible text

cy.contains("Save").click()

The runner

CallMeaning
cy.visit("/cart")Loads that path. A baseUrl in config is prefixed
cy.getA CSS selector. The query retries until the command timeout
cy.containsAn element whose text includes the string
clickActs on one element. Several matches is an error
QueueThe call is enqueued. The next line is not a DOM node
Where it runsInside the browser, beside the app

This page is Cypress. Selenium is left out.

Queries

The attribute Cypress recommends

# cy.get("[data-cy=qty]")

What the selector hits

SelectorMatches
[data-cy=save]A hook you add for tests
#saveAn id
.qtyA class
button.primaryA button that also has that class
cy.contains("button", "Save")A button whose text includes Save
.eq(0)The first match. This index starts at 0. A CSS :nth-child index starts at 1

Assertions

Retry until the count matches

cy.get("[data-cy=row]").should("have.length", 3)

should keeps querying

AssertionPasses when
have.lengthThe number of matches equals the value. The query retries
have.textThe text is exactly that string
containThe text includes the string
be.visibleThe element is shown
have.valueThe input value equals the string
not.existNothing matches. This retries until the timeout too

Click and type

Replace the field

cy.get("[data-cy=qty]").clear().type("2")

One row

cy.get("[data-cy=row]").first().click()

Actionability

CallEffect
clickClicks one element that is visible, enabled, and not covered
{ multiple: true }Clicks each match, one after another
{ force: true }Skips the covered, hidden, and disabled checks
typeTypes the characters. Text already in the field stays until clear
clearEmpties an input or textarea
firstNarrows the list to one element before the action

The command queue

Read the node inside then

cy.get("[data-cy=name]").then((el) => {
  const name = el.text()
})

Run the saved query again

cy.get("@rows").first().click()

What you can store

PieceMeaning
Return valuecy.get does not hand you the element. Chain, or use then
thenRuns once when the subject is ready. Code inside it is not retried
.as("rows")Saves the query. cy.get("@rows") runs that query again
this.rowsA snapshot from when as ran. An arrow function has no Mocha this
After clickQuery again. The click may have replaced that node
awaitA Cypress command is queued. The docs treat it as something you do not await

Intercepts

Name the request

cy.intercept("POST", "/api/cart").as("saveCart")

Continue after the response

cy.wait("@saveCart")

Order matters

CallMeaning
cy.interceptWatches or stubs a request. Register it before the app sends that request
MethodPass GET or POST. With no method, every method matches
.as("saveCart")The name cy.wait uses
cy.wait("@saveCart")Continues after that response
Static bodycy.intercept(method, url, { id: 1 }) replies without the real server
cy.routeRemoved in Cypress 12, along with cy.server. Use cy.intercept

Fixtures

Load once per test

cy.fixture("user.json").as("user")

How to read it later

PieceMeaning
cy.fixtureReads a file from the fixtures folder and parses JSON
.as("user")Stores it for cy.get("@user")
this.userSet on a function () test only after the alias command has run
Arrow itthis.user is missing. Use cy.get("@user")
beforeEachLoad the fixture here so every test gets it
ClearedAliases are wiped before the next test

Timeouts

A pause is not a retry

# cy.wait(1000) only pauses

Which clock

LimitDefault
defaultCommandTimeout4 seconds. Queries and most commands use it
Per command{ timeout: 10000 } on that cy.get waits 10 seconds
cy.wait(1000)A fixed pause. No condition is retried
cy.wait("@alias")Waits for the response. The default response timeout is 30 seconds
cy.visitWaits for the load event. A request the page makes afterward needs its own intercept
A missing elementThe test uses the full command timeout, then fails

Each test

A clean start

PieceMeaning
itOne test. describe groups tests
beforeEachRuns before each test. Put cy.visit and cy.intercept here
testIsolationDefault true since Cypress 12. Cookies, localStorage, and sessionStorage are cleared, and the page is blanked
Also clearedAliases, intercepts, stubs, and clock mocks are reset before each test
it.onlyLimits this spec file. Other spec files still run under cypress run
IndexedDBLeft in place. testIsolation does not clear it

Interview traps

Let should retry

# cy.get("[data-cy=save]").should("be.visible")

Intercept, then visit

# cy.intercept("GET", "/api/cart") before cy.visit("/cart")

Traps worth memorising

TrapWhat happensPrefer
const el = cy.get(...)el is the queued command, not the DOM nodeA chain, or then when you need the node
expect inside thenThe expect runs once. A later DOM change is missed.should on the query, so Cypress retries
Intercept after visitThe page request already left, so the wait times outcy.intercept before cy.visit
cy.routeRemoved in Cypress 12cy.intercept
force: trueThe click skips covered, hidden, and disabled checksA normal click on one matching element
this.user in an arrowMocha does not set this on an arrow functionfunction () or cy.get("@user")

How to use this page: query with cy.get, assert with should so the query retries, and register cy.intercept before the page asks for data.