Cypress

From G1Joshi/Agent-Skills · View interactive version

Cypress is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Cypress testing assistance covering end-to-end, component testing, and intercepting network calls. Use when writing E2E tests, automating user workflows, or mocking network endpoints.

How to use this skill

Skill file

Cypress

Cypress is a next generation front end testing tool built for the modern web. It runs _inside_ the browser (unlike Selenium/Playwright) which gives it native access to the DOM.

When to Use

Quick Start

describe("My First Test", () => {
  it("Visits the Kitchen Sink", () => {
    cy.visit("https://example.cypress.io");
    cy.contains("type").click();
    cy.url().should("include", "/commands/actions");
    cy.get(".action-email")
      .type("fake@email.com")
      .should("have.value", "fake@email.com");
  });
});

Core Concepts

Asynchronous Command Queuing & Automatic Retries

Cypress commands do not return standard promises; they queue actions that automatically retry until assertions pass or timeout:

// cypress/e2e/login.cy.ts
describe("Authentication Flow", () => {
  it("logs in user and displays dashboard", () => {
    cy.visit("/login");

    // Automatically retries until element exists and is visible
    cy.get('input[name="email"]').type("jane@example.com");
    cy.get('input[name="password"]').type("Password123!");
    cy.get('button[type="submit"]').click();

    // Asserts on URL and DOM content
    cy.url().should("include", "/dashboard");
    cy.contains("h1", "Welcome back, Jane").should("be.visible");
  });
});

Network Interception & Dynamic Fixture Stubbing (`cy.intercept`)

Controls and mocks network traffic without external proxy dependencies:

it("handles network failure gracefully", () => {
  cy.intercept("GET", "/api/v1/projects", {
    statusCode: 500,
    body: { error: "Internal Server Error" },
  }).as("getProjectsError");

  cy.visit("/projects");
  cy.wait("@getProjectsError");

  cy.get(".error-banner").should("contain.text", "Failed to load projects");
});

Custom Commands & Page Object Encapsulation

Encapsulates reusable user workflows:

// cypress/support/commands.ts
Cypress.Commands.add("loginViaApi", (email: string, password: string) => {
  cy.request("POST", "/api/v1/auth/login", { email, password }).then((res) => {
    window.localStorage.setItem("authToken", res.body.token);
  });
});

Common Patterns

Network Request Interception and Waiting

Problem: Flaky tests caused by asserting UI before backend API responses have completed and rendered.

Solution:

Use cy.intercept with route aliases and cy.wait:

it("loads and displays user profile data", () => {
  cy.intercept("GET", "/api/v1/user", { fixture: "user.json" }).as("getUser");
  cy.visit("/dashboard");
  cy.wait("@getUser");
  cy.get('[data-cy="user-name"]').should("contain.text", "Jane Doe");
});

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------------- | :------------------------------------------------------ | :----------------------------------------------------------------------------------- |

| CypressError: Timed out retrying after 4000ms | Element not found in DOM or covered by another element. | Check selector specificity and wait for loading spinners to detach. |

| cy.visit() failed trying to load | Target dev server is down or SSL certificate untrusted. | Verify base URL in cypress.config.js and set chromeWebSecurity: false if needed. |

| Cannot read properties of undefined (reading 'as') | Chaining assertions incorrectly on cy.intercept. | Assign .as('alias') immediately on cy.intercept(...) declaration. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.