Skip to content

Cypress チートシート

Web アプリケーションのためのエンドツーエンド(E2E)テストフレームワーク。

01

はじめに

基本のテスト

Cypress はブラウザ上で動作します。

cypress
describe("Home", () => {
  it("loads", () => {
    cy.visit("/");
    cy.contains("Welcome");
  });
});

ユーザー操作

ユーザー操作のためのチェーン可能なコマンド群。

cypress
cy.get("input").type("hello");
cy.get("button").click();
cy.get(".result").should("contain", "hello");

アサーション

アサーションには should() を使います。

cypress
cy.get("#count").should("have.text", "5");
cy.get("li").should("have.length", 3);
cy.url().should("include", "/dashboard");
02

主な機能

フィクスチャ

テストデータにはフィクスチャを使います。

cypress
cy.fixture("user.json").then(user => {
  cy.get("input").type(user.name);
});

カスタムコマンド

再利用可能なステップをカスタムコマンドとして定義できます。

cypress
Cypress.Commands.add("login", (email, pw) => {
  cy.get("#email").type(email);
  cy.get("#pw").type(pw);
  cy.get("button").click();
});

cy.login("[email protected]", "secret");

Was this helpful?