E2E Testing with Claude Code: Playwright/Cypress Practical Guide
E2E Testing using Claude Code. Playwright/Cypress Practical Guide. Includes practical code examples.
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 の使い分け
| 項目 | Playwright | Cypress |
|---|---|---|
| マルチブラウザ | Chromium/Firefox/WebKit | Chrome/Firefox/Edge |
| 並列実行 | ネイティブ対応 | Dashboard利用 |
| マルチタブ | 対応 | 非対応 |
| APIテスト | 対応 | 対応 |
| 学習コスト | やや高い | 低い |
ビジュアルリグレッションテスト
Playwrightのスクリーンショット比較機能を使えば、UIの意図しない変更を検出できます。
test('ヘッダーの見た目が変わっていないか確認', async ({ page }) => {
await page.goto('/');
await expect(page.locator('header')).toHaveScreenshot('header.png');
});
Zusammenfassung
Claude Codeを使えば、PlaywrightやCypressのE2Eテストを効率的に実装し、品質の高いテストスイートを構築できます。APIテスト自動化やテスト戦略ガイドも合わせて参考にしてください。
詳細はPlaywright公式ドキュメントおよびCypress公式ドキュメントを参照してください。
Related Posts
So beschleunigen Sie Ihre Nebenprojekte mit Claude Code [Mit Beispielen]
Erfahren Sie, wie Sie persönliche Entwicklungsprojekte mit Claude Code drastisch beschleunigen. Inklusive realer Beispiele und eines praktischen Workflows von der Idee bis zum Deployment.
So automatisieren Sie Refactoring mit Claude Code
Erfahren Sie, wie Sie Code-Refactoring mit Claude Code effizient automatisieren. Inklusive praktischer Prompts und konkreter Refactoring-Muster für reale Projekte.
Vollständiger CORS-Konfigurationsleitfaden mit Claude Code
Erfahren Sie alles über die CORS-Konfiguration mit Claude Code. Mit praktischen Tipps und Codebeispielen.