Use Cases

E2E Testing with Claude Code: Playwright/Cypress 实战指南

E2E Testing:Claude Code 实战. Playwright/Cypress Practical Guide. 包含实用代码示例。

E2E 测试を通过 Claude Code 提高效率

E2E(End-to-End)测试は用户の操作フローを自動验证する重要な测试手法です。借助 Claude Code,PlaywrightやCypressの测试コードを快速生成し、CI環境への集成まで一気に進められます。

Playwrightでの测试实现

日志インフローの测试

> 日志インからダッシュボード显示までのE2E 测试を
> Playwrightで创建して。Page Objectパターンで。
// tests/pages/LoginPage.ts
import { Page, Locator } from '@playwright/test';

export class LoginPage {
  readonly page: Page;
  readonly emailInput: Locator;
  readonly passwordInput: Locator;
  readonly submitButton: Locator;
  readonly errorMessage: Locator;

  constructor(page: Page) {
    this.page = page;
    this.emailInput = page.getByLabel('メールアドレス');
    this.passwordInput = page.getByLabel('パスワード');
    this.submitButton = page.getByRole('button', { name: 'ログイン' });
    this.errorMessage = page.getByRole('alert');
  }

  async goto() {
    await this.page.goto('/login');
  }

  async login(email: string, password: string) {
    await this.emailInput.fill(email);
    await this.passwordInput.fill(password);
    await this.submitButton.click();
  }
}
// tests/e2e/login.spec.ts
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';

test.describe('ログイン機能', () => {
  test('正しい認証情報でログインできる', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();
    await loginPage.login('[email protected]', 'password123');

    await expect(page).toHaveURL('/dashboard');
    await expect(page.getByText('ダッシュボード')).toBeVisible();
  });

  test('不正な認証情報でエラーが表示される', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();
    await loginPage.login('[email protected]', 'wrongpassword');

    await expect(loginPage.errorMessage).toBeVisible();
    await expect(loginPage.errorMessage).toContainText('認証に失敗しました');
  });
});

Cypressでの测试实现

ECサイトの購入フロー

> 商品选择からカート添加、購入完成までのフローを
> Cypressで创建して。カスタムコマンドも定義して。
// cypress/support/commands.ts
Cypress.Commands.add('login', (email: string, password: string) => {
  cy.session([email, password], () => {
    cy.visit('/login');
    cy.get('[data-cy="email"]').type(email);
    cy.get('[data-cy="password"]').type(password);
    cy.get('[data-cy="submit"]').click();
    cy.url().should('include', '/dashboard');
  });
});

// cypress/e2e/purchase.cy.ts
describe('購入フロー', () => {
  beforeEach(() => {
    cy.login('[email protected]', 'password123');
  });

  it('商品をカートに追加して購入完了できる', () => {
    cy.visit('/products');
    cy.get('[data-cy="product-card"]').first().click();
    cy.get('[data-cy="add-to-cart"]').click();
    cy.get('[data-cy="cart-badge"]').should('contain', '1');

    cy.visit('/cart');
    cy.get('[data-cy="checkout-button"]').click();

    cy.get('[data-cy="confirm-order"]').click();
    cy.get('[data-cy="order-complete"]').should('be.visible');
    cy.get('[data-cy="order-number"]').should('exist');
  });
});

Playwright vs Cypress の使い分け

項目PlaywrightCypress
マルチ浏览器Chromium/Firefox/WebKitChrome/Firefox/Edge
并行実行ネイティブ支持Dashboard利用
マルチ标签页支持非支持
API测试支持支持
学習コストやや高い低い

ビジュアルリグレッション测试

Playwrightのスクリーンショット比較機能を使えば、UIの意図しない更改を検出可以。

test('ヘッダーの見た目が変わっていないか確認', async ({ page }) => {
  await page.goto('/');
  await expect(page.locator('header')).toHaveScreenshot('header.png');
});

总结

借助 Claude Code,PlaywrightやCypressのE2E 测试を高效地实现し、品質の高い测试スイートを构建可以。API测试自动化测试戦略指南也建议一并参考。

详细信息请参阅Playwright官方文档およびCypress官方文档

#Claude Code #E2Eテスト #Playwright #Cypress #automation