Use Cases

E2E Testing with Claude Code: Playwright/Cypress Practical Guide

E2E Testing Claude Code का उपयोग करके. Playwright/Cypress Practical Guide. Includes practical code examples.

E2Etestको Claude Code से Efficient बनाएं

E2E(End-to-End)testはuserの操作フローを自動検証するimportantなtest手法 है।Claude Code का उपयोग करके、PlaywrightやCypressのtestcodeを素早くgenerateし、CI環境へのintegration तक一気に進められ है।

Playwrightでのtestimplementation

logインフローのtest

> logイン सेdashboarddisplay तकのE2Etestを
> Playwrightでबनाओ。Page Objectpatternで。
// 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('pathワード');
    this.submitButton = page.getByRole('button', { name: 'logイン' });
    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('logインfeatures', () => {
  test('सहीauthentication情報でlogインできる', 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('dashboard')).toBeVisible();
  });

  test('不正なauthentication情報でerrorがdisplayされる', 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('authenticationに失敗しました');
  });
});

Cypressでのtestimplementation

ECサイトの購入フロー

> 商品選択 सेcartadd、購入完了 तकのフローを
> Cypressでबनाओ。カスタムcommandも定義して。
// 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('商品をcartにadd करो購入完了できる', () => {
    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
並列実行ネイティブsupportDashboard利用
マルチtabsupport非support
APItestsupportsupport
学習コストやや高い低い

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

Playwrightのスクリーンショット比較featuresを使えば、UIの意図しない変更を検出でき है।

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

Summary

Claude Code का उपयोग करके、PlaywrightやCypressのE2Etestをefficientlyimplementationし、品質の高いtestスイート build किया जा सकता है。APItestautomationtest戦略ガイドも合わせてreference के लिए देखें。

詳細はPlaywrightofficial documentationおよびCypressofficial documentationをदेखें。

#Claude Code #E2Etest #Playwright #Cypress #automation