liminfo

Cypress Reference

Free reference guide: Cypress Reference

44 results

About Cypress Reference

The Cypress Reference is a practical, searchable cheat sheet covering everything you need to write reliable end-to-end tests with Cypress. It spans 8 categories: Commands (cy.visit, cy.get, cy.type, cy.click, cy.select, cy.scrollTo), Selectors (data-testid, cy.find, cy.first, cy.eq, cy.parent, cy.closest), Assertions (should("be.visible"), should("have.text"), should("have.length"), should("have.attr")), Intercept (cy.intercept with stubs, error mocking, delay simulation), Fixtures, Custom Commands, Config, and Best Practices. Every entry includes a real test code example so you can copy and adapt immediately.

This reference is aimed at frontend developers, QA engineers, and full-stack teams adopting test-driven development with Cypress. The Intercept section is especially valuable for teams testing API-dependent UIs — it shows how to stub responses, mock error states (statusCode: 500), simulate slow networks with delay, and wait for specific requests with aliasing (cy.wait("@alias")). The Fixtures section demonstrates how to load JSON test data and combine it with cy.intercept for repeatable, isolated API mocking.

The Custom Commands section shows how to encapsulate repeated actions — such as login flows — into reusable cy.login() calls using Cypress.Commands.add(), and how to override built-in commands with Cypress.Commands.overwrite(). The Best Practices section covers the most important Cypress conventions: using data-testid attributes for stable selectors, seeding state via API rather than UI, avoiding conditional testing, and caching login sessions with cy.session() to speed up test suites.

Key Features

  • Covers 8 categories: Commands, Selectors, Assertions, Intercept, Fixtures, Custom Commands, Config, Best Practices
  • Full cy.intercept API: response stubs, error mocking (statusCode: 500), delay simulation, and request aliasing
  • All major assertions: be.visible, have.text, have.value, have.class, have.length, exist, contain, have.attr
  • Selector strategies: data-testid, cy.find, cy.eq, cy.first/last, cy.parent/children, cy.closest
  • Fixture-based API mocking with cy.fixture, cy.readFile, cy.writeFile, and fixture + intercept patterns
  • Custom command patterns: Cypress.Commands.add() for login flows and Cypress.Commands.overwrite()
  • Configuration: cypress.config.js setup, env variables, Cypress.env() access, and retries config
  • Best practice patterns: data-testid usage, API seeding in beforeEach, and cy.session() for login caching

Frequently Asked Questions

What is cy.intercept() and when should I use it?

cy.intercept() intercepts network requests made by the application during a test. You can use it to stub responses (return fake data without hitting a real server), mock error conditions (statusCode: 500), simulate slow networks (delay: 2000), or simply observe whether a request was made. It replaces the older cy.route() and is the standard way to control API behavior in Cypress tests. Always alias intercepted requests with .as("name") so you can wait for them with cy.wait("@name").

Why should I use data-testid attributes instead of CSS selectors?

CSS class names and element structures change frequently during UI development. Using data-testid="submit-btn" attributes creates a stable contract between your application and your tests. They are invisible to users, never affected by styling changes, and make test intentions explicit. This is one of Cypress's official best practices. You can also use aria attributes like aria-label when they convey semantic meaning to users.

How do I handle login in Cypress without repeating the login flow in every test?

The best approach is cy.session(), which caches the session state (cookies, localStorage, sessionStorage) after the first login and restores it for subsequent tests. This avoids repeating the login UI flow on every test run. Alternatively, you can create a custom cy.login() command using Cypress.Commands.add() that performs the login via API (cy.request()) rather than through the UI, which is faster and more reliable.

What is the difference between cy.get() and cy.find()?

cy.get() searches the entire document from the root, while cy.find() searches within the previously selected element. For example, cy.get(".parent").find("li") finds all li elements inside .parent, whereas cy.get("li") would find all li elements anywhere on the page. Use cy.find() to scope your queries to a specific container, which makes tests more precise and resilient to changes elsewhere on the page.

How do I test API error states with Cypress?

Use cy.intercept() with a statusCode of 400, 401, 403, 500, or any error code to simulate error responses. For example: cy.intercept("GET", "/api/data", { statusCode: 500, body: { error: "Server Error" } }) will cause the application to receive a 500 error when it requests /api/data. You can then assert that error messages appear correctly in the UI. This lets you test error handling without needing a real failing server.

How do Cypress fixtures work?

Fixtures are static JSON files stored in the cypress/fixtures directory. cy.fixture("users.json") loads the file and returns its parsed contents in a .then() callback. You can use fixtures directly with cy.intercept() by passing { fixture: "users.json" } as the response body, which automatically loads and returns the fixture data. cy.readFile() and cy.writeFile() let you read and write arbitrary files during tests, useful for dynamic data generation.

What is API seeding and why is it better than UI setup?

API seeding means setting up test data and application state through direct API calls (cy.request()) in beforeEach, rather than clicking through the UI. This is faster, more reliable, and isolates the test from UI bugs. For example, instead of navigating to an admin panel to create a user before every test, you call cy.request("POST", "/api/users", { name: "test" }) directly. The Cypress best practices guide recommends API seeding as the primary way to establish test preconditions.

How do I configure Cypress for different environments?

Use cypress.config.js (or cypress.config.ts for TypeScript) to set baseUrl, viewport dimensions, and default timeout values. Environment-specific values (API URLs, credentials) go in the env object within defineConfig(). Access them at runtime with Cypress.env("key"). For CI/CD pipelines, you can override env values by passing --env key=value on the command line. Use the retries configuration (runMode: 2, openMode: 0) to automatically retry flaky tests in CI without impacting local development feedback.