web-testing-playwright-e2e
Playwright E2Eテストのパターンとして、テスト構造やページオブジェクトモデル、ロケーター戦略、アサーション、ネットワークモック、Visual Regressionなどを活用し、並列実行や設定を効率的に行うSkill。
📜 元の英語説明(参考)
Playwright E2E testing patterns - test structure, Page Object Model, locator strategies, assertions, network mocking, visual regression, parallel execution, fixtures, and configuration
🇯🇵 日本人クリエイター向け解説
Playwright E2Eテストのパターンとして、テスト構造やページオブジェクトモデル、ロケーター戦略、アサーション、ネットワークモック、Visual Regressionなどを活用し、並列実行や設定を効率的に行うSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o web-testing-playwright-e2e.zip https://jpskill.com/download/10312.zip && unzip -o web-testing-playwright-e2e.zip && rm web-testing-playwright-e2e.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10312.zip -OutFile "$d\web-testing-playwright-e2e.zip"; Expand-Archive "$d\web-testing-playwright-e2e.zip" -DestinationPath $d -Force; ri "$d\web-testing-playwright-e2e.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
web-testing-playwright-e2e.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
web-testing-playwright-e2eフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
Playwright E2E テストのパターン
クイックガイド: Playwright を使用して、実際のブラウザを通して完全なユーザーワークフローを検証するエンドツーエンドテストを行います。重要なユーザー体験に焦点を当て、アクセシビリティに基づいたロケーター (
getByRole) を使用し、自動待機アサーションを活用してください。手動のスリープは絶対に使用しないでください。各テストを独自のブラウザコンテキストで分離します。信頼性を高めるために、ルートインターセプションを介して外部 API をモックします。
<critical_requirements>
重要: この Skill を使用する前に
すべてのコードは CLAUDE.md のプロジェクト規約に従う必要があります (kebab-case、名前付きエクスポート、インポート順序、
import type、名前付き定数)
(主要なロケーター戦略として getByRole() を必ず使用してください - これはユーザーがページを操作する方法を反映しています)
(完全なユーザーワークフローをエンドツーエンドで必ずテストしてください - ログインフロー、チェックアウトプロセス、フォーム送信)
(自動待機する Web-first アサーションを必ず使用してください - toBeVisible()、toHaveText()、手動のスリープは不要です)
(テストを必ず分離してください - 各テストは独自のブラウザコンテキストで独立して実行されます)
(テストデータには必ず名前付き定数を使用してください - テストファイルにマジックストリングや数値を使用しないでください)
</critical_requirements>
自動検出: Playwright, E2E testing, end-to-end testing, browser automation, page.goto, test.describe, expect(page), getByRole, getByTestId, toBeVisible, toHaveScreenshot, toMatchAriaSnapshot
使用する場面:
- 重要なユーザー向けワークフローのテスト (ログイン、チェックアウト、フォーム送信)
- 複数のページにまたがる複数ステップのユーザー体験
- クロスブラウザ互換性テスト
- バックエンド API との実際の統合テスト
- スクリーンショットを使用したビジュアルリグレッションテスト
- ARIA スナップショットを使用したアクセシビリティツリーの検証
使用しない場面:
- 純粋なユーティリティ関数のテスト (ユニットテストを使用)
- 個々のコンポーネントバリアントの分離されたテスト (コンポーネントテストツールを使用)
- UI なしの API のみのテスト (API テストを使用)
カバーする主要なパターン:
- テスト構造と構成 (test.describe, test, hooks)
- メンテナンス性のための Page Object Model パターン
- アクセシビリティを優先するロケーター戦略 (getByRole, getByLabel, chaining, operators)
- 自動待機する Web-first アサーション (toBeVisible, toHaveText, soft assertions)
- ネットワークのモックとインターセプト (route.fulfill, route.abort, response modification)
- ビジュアルリグレッションテスト (toHaveScreenshot, masking, thresholds)
- カスタムおよびワーカー スコープのフィクスチャ
- 時間依存テストのための Clock API (v1.45+)
- アクセシビリティのための ARIA スナップショットテスト (v1.49+)
- アクセシビリティアサーション (v1.44+): toHaveAccessibleName, toHaveRole
詳細なリソース:
- examples/core.md - ユーザーフロー、ページオブジェクト、ネットワークモック、設定、認証フィクスチャ
- examples/page-objects.md - ベースページの継承、階層
- examples/api-mocking.md - レスポンスのインターセプトと変更
- examples/visual-testing.md - スクリーンショットの比較、コンポーネントのビジュアルテスト
- examples/fixtures.md - 結合されたフィクスチャ、データベースのシーディング、IndexedDB の状態
- examples/advanced-features.md - Clock API、ARIA スナップショット、ワーカーフィクスチャ、ポーリングアサーション、アクセシビリティアサーション
- reference.md - 意思決定フレームワーク、ロケーター/アサーションテーブル、アンチパターン、CLI コマンド、構成リファレンス
<philosophy>
Philosophy
Playwright E2E テストは、アプリケーションがユーザーの視点から正しく動作することを検証します。実際のブラウザを操作し、実際のページをナビゲートし、ユーザーに見える動作を検証します。
コア原則:
- ユーザーに見える動作をテストする - 実装の詳細ではなく、エンドユーザーが見て操作するものに焦点を当てます
- アクセシビリティロケーターを使用する -
getByRoleは、スクリーンリーダーやユーザーがページを操作する方法を反映しています - テストを完全に分離する - 各テストには、独自のブラウザコンテキスト、Cookie、およびストレージがあります
- 自動待機を信頼する - Playwright は要素を自動的に待機します。手動のスリープは必要ありません
- 外部依存関係をモックする - サードパーティ API にはルートインターセプションを使用して、信頼性を確保します
E2E テストが最も価値を提供する場面:
- 重要なビジネスワークフロー (認証、支払い、コア機能)
- 複数のページまたはコンポーネントにまたがるユーザー体験
- 実際のバックエンド統合のテスト
- クロスブラウザ互換性の検証
- ユニットテストでは見逃される統合バグの検出
E2E テストが最適な選択肢ではない可能性がある場面:
- 純粋なユーティリティ関数のテスト (ユニットテストの方が高速で正確です)
- コンポーネントスタイリングバリアントのテスト (ビジュアルテストツールを使用)
- すべてのエッジケースのテスト (ユニットテストおよび統合テストとのバランスを取る)
</philosophy>
<patterns>
コアパターン
パターン 1: テスト構造と構成
関連するテストを test.describe でグループ化し、共通のナビゲーションには beforeEach を使用し、すべてのテストデータには名前付き定数を使用します。
const LOGIN_URL = "/login";
const VALID_EMAIL = "user@example.com";
test.describe("Login Flow", () => {
test.beforeEach(async ({ page }) => {
await page.goto(LOGIN_URL);
});
test("successful login redirects to dashboard", async ({ page }) => {
await page.getByLabel(/email/i).fill(VALID_EMAIL);
// ... パスワードを入力し、サインインをクリックします
await expect(page).toHaveURL("/dashboard");
});
});
良い理由: 関連するテストを論理的にグループ化し、beforeEach は分離を維持し、名前付き定数はマジックストリングを防ぎます
エラーシナリオを含む完全なユーザーフローについては、examples/core.md パターン 1 を参照してください。
パターン 2: Page Object Model
ページ構造とインタラクションを再利用可能なクラスにカプセル化します。コンストラクターでロケーターを定義し、ドメイン固有のメソッドを公開します。
export class LoginPage {
readonly emailInput: Locator;
readonly signInButton: Locator;
constructor(page: Page) {
this.emailInput = page.getByLabel(/email/i);
this.signInButton = page.getByRole("button", { name: /sign in/i });
}
async log
(原文がここで切り詰められています) 📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Playwright E2E Testing Patterns
Quick Guide: Use Playwright for end-to-end tests that verify complete user workflows through the real browser. Focus on critical user journeys, use accessibility-based locators (
getByRole), and leverage auto-waiting assertions -- never use manual sleeps. Isolate each test with its own browser context. Mock external APIs via route interception for reliability.
<critical_requirements>
CRITICAL: Before Using This Skill
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST use getByRole() as your primary locator strategy - it mirrors how users interact with the page)
(You MUST test complete user workflows end-to-end - login flows, checkout processes, form submissions)
(You MUST use web-first assertions that auto-wait - toBeVisible(), toHaveText(), not manual sleeps)
(You MUST isolate tests - each test runs independently with its own browser context)
(You MUST use named constants for test data - no magic strings or numbers in test files)
</critical_requirements>
Auto-detection: Playwright, E2E testing, end-to-end testing, browser automation, page.goto, test.describe, expect(page), getByRole, getByTestId, toBeVisible, toHaveScreenshot, toMatchAriaSnapshot
When to use:
- Testing critical user-facing workflows (login, checkout, form submission)
- Multi-step user journeys that span multiple pages
- Cross-browser compatibility testing
- Testing real integration with backend APIs
- Visual regression testing with screenshots
- Accessibility tree validation with ARIA snapshots
When NOT to use:
- Testing pure utility functions (use unit tests)
- Testing individual component variants in isolation (use component testing tools)
- API-only testing without UI (use API testing)
Key patterns covered:
- Test structure and organization (test.describe, test, hooks)
- Page Object Model pattern for maintainability
- Locator strategies prioritizing accessibility (getByRole, getByLabel, chaining, operators)
- Web-first assertions with auto-waiting (toBeVisible, toHaveText, soft assertions)
- Network mocking and interception (route.fulfill, route.abort, response modification)
- Visual regression testing (toHaveScreenshot, masking, thresholds)
- Custom and worker-scoped fixtures
- Clock API for time-dependent testing (v1.45+)
- ARIA snapshot testing for accessibility (v1.49+)
- Accessibility assertions (v1.44+): toHaveAccessibleName, toHaveRole
Detailed Resources:
- examples/core.md - User flows, page objects, network mocking, config, auth fixtures
- examples/page-objects.md - Base page inheritance, hierarchy
- examples/api-mocking.md - Response interception and modification
- examples/visual-testing.md - Screenshot comparison, component visual testing
- examples/fixtures.md - Combined fixtures, database seeding, IndexedDB state
- examples/advanced-features.md - Clock API, ARIA snapshots, worker fixtures, polling assertions, accessibility assertions
- reference.md - Decision frameworks, locator/assertion tables, anti-patterns, CLI commands, configuration reference
<philosophy>
Philosophy
Playwright E2E tests verify that your application works correctly from the user's perspective. They interact with the real browser, navigate through actual pages, and validate user-visible behavior.
Core Principles:
- Test user-visible behavior - Focus on what end users see and interact with, not implementation details
- Use accessibility locators -
getByRolemirrors how screen readers and users interact with pages - Isolate tests completely - Each test has its own browser context, cookies, and storage
- Trust auto-waiting - Playwright automatically waits for elements; no manual sleeps needed
- Mock external dependencies - Use route interception for third-party APIs to ensure reliability
When E2E tests provide the most value:
- Critical business workflows (authentication, payments, core features)
- User journeys spanning multiple pages or components
- Testing real backend integration
- Cross-browser compatibility verification
- Catching integration bugs that unit tests miss
When E2E tests may not be the best choice:
- Testing pure utility functions (unit tests are faster and more precise)
- Testing component styling variants (use visual testing tools)
- Testing every edge case (balance with unit and integration tests)
</philosophy>
<patterns>
Core Patterns
Pattern 1: Test Structure and Organization
Group related tests with test.describe, use beforeEach for common navigation, and name constants for all test data.
const LOGIN_URL = "/login";
const VALID_EMAIL = "user@example.com";
test.describe("Login Flow", () => {
test.beforeEach(async ({ page }) => {
await page.goto(LOGIN_URL);
});
test("successful login redirects to dashboard", async ({ page }) => {
await page.getByLabel(/email/i).fill(VALID_EMAIL);
// ... fill password, click sign in
await expect(page).toHaveURL("/dashboard");
});
});
Why good: Groups related tests logically, beforeEach maintains isolation, named constants prevent magic strings
See examples/core.md Pattern 1 for complete user flow with error scenarios.
Pattern 2: Page Object Model
Encapsulate page structure and interactions in reusable classes. Define locators in the constructor, expose domain-specific methods.
export class LoginPage {
readonly emailInput: Locator;
readonly signInButton: Locator;
constructor(page: Page) {
this.emailInput = page.getByLabel(/email/i);
this.signInButton = page.getByRole("button", { name: /sign in/i });
}
async login(email: string, password: string) {
await this.emailInput.fill(email);
// ...
}
}
Why good: Centralizes locators -- UI changes update one place, methods encapsulate interactions
When to use: Tests spanning multiple interactions on the same page, reusable flows across test files.
When not to use: Simple one-off tests where inline locators are clearer.
See examples/core.md Pattern 2 for full page objects with fixtures, examples/page-objects.md for base page inheritance.
Pattern 3: Locator Strategies
Prioritize accessibility-based locators that mirror how users interact with the application.
// BEST: Accessibility-based
await page.getByRole("button", { name: /submit/i });
await page.getByLabel(/email address/i);
await page.getByText(/welcome back/i);
// ACCEPTABLE: Test IDs for complex cases
await page.getByTestId("user-avatar"); // When no semantic role exists
// AVOID: Implementation-dependent
await page.locator("#submit-btn"); // Fragile
await page.locator(".btn-primary"); // CSS class can change
Chaining and filtering narrow down to specific elements without fragile selectors:
await page
.getByRole("listitem")
.filter({ hasText: "Product A" })
.getByRole("button", { name: /add to cart/i })
.click();
// Exclude elements (v1.33+)
await page
.getByRole("listitem")
.filter({ hasNot: page.getByText("Out of stock") })
.first()
.click();
// Combine conditions (v1.33+)
const btn = page.getByRole("button").and(page.getByTitle("Subscribe"));
Why good: getByRole validates accessibility as a side effect, survives UI refactoring, chaining handles dynamic lists
See reference.md for locator priority table and common ARIA role mappings.
Pattern 4: Web-First Assertions
Use assertions that automatically wait and retry until the condition is met.
// Auto-waits for element visibility
await expect(page.getByText("Welcome")).toBeVisible();
// Auto-waits for URL
await expect(page).toHaveURL(/\/dashboard/);
// Negated assertions also auto-wait
await expect(page.getByRole("progressbar")).not.toBeVisible();
Why good: Eliminates flaky tests from race conditions, no manual sleeps needed
// BAD: Manual waiting
await page.waitForTimeout(2000); // Arbitrary sleep!
const text = await page.textContent(".result");
expect(text).toBe("Success"); // Non-waiting assertion
Why bad: Fixed timeouts are either too short (flaky) or too long (slow), doesn't adapt to actual page load time
Soft assertions collect all failures in one run:
await expect.soft(page.getByTestId("avatar")).toBeVisible();
await expect.soft(page.getByText("Premium")).toBeVisible();
// Test continues, all failures reported at end
See reference.md for complete assertion table, polling assertions, and accessibility assertions (v1.44+).
Pattern 5: Network Mocking and Interception
Mock external APIs for reliable, isolated tests. Use page.route() to intercept and fulfill requests.
const API_USERS = "**/api/users";
await page.route(API_USERS, (route) =>
route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({ id: "user-123", name: "John Doe" }),
}),
);
// Error simulation
await page.route(API_USERS, (route) => route.abort("failed")); // Network failure
Why good: Eliminates third-party flakiness, enables testing error states, controls exact data
Modifying real responses (hybrid approach):
await page.route("**/api/products", async (route) => {
const response = await route.fetch();
const json = await response.json();
json.products = json.products.map((p: { price: number }) => ({
...p,
price: p.price * 0.9,
}));
await route.fulfill({ response, json });
});
See examples/core.md Pattern 3 for complete mocking with error states, examples/api-mocking.md for response modification.
Pattern 6: Visual Regression Testing
Capture and compare screenshots to detect unintended visual changes.
await expect(page).toHaveScreenshot("homepage.png");
// Mask dynamic content
await expect(page).toHaveScreenshot("dashboard.png", {
mask: [page.getByTestId("current-time"), page.getByTestId("random-ad")],
});
// Disable animations for deterministic screenshots
await expect(page).toHaveScreenshot("stable.png", { animations: "disabled" });
Why good: Catches unintended visual changes, masking prevents false positives from timestamps
See examples/visual-testing.md for component visual testing with state variations.
Pattern 7: Custom Fixtures
Extend the base test with reusable fixtures for page objects, authentication, and shared setup.
export const test = base.extend<{
loginPage: LoginPage;
authenticatedPage: void;
}>({
loginPage: async ({ page }, use) => {
await use(new LoginPage(page));
},
authenticatedPage: [
async ({ context }, use) => {
await context.addCookies([
{ name: "session", value: "token", domain: "localhost", path: "/" },
]);
await use();
await context.clearCookies();
},
{ auto: true },
],
});
Why good: Encapsulates setup + teardown, auto fixtures eliminate repetitive auth, composable
See examples/core.md Pattern 5 for auth fixtures, examples/fixtures.md for combined fixtures and database seeding, examples/advanced-features.md for worker-scoped fixtures.
Pattern 8: Clock API (v1.45+)
Control time for testing countdowns, session timeouts, and scheduled events.
await page.clock.install({ time: new Date("2024-02-02T08:00:00") });
await page.goto("/dashboard");
await page.clock.fastForward("25:00"); // Jump 25 minutes
await expect(page.getByText(/session expires/i)).toBeVisible();
CRITICAL: clock.install() MUST be called before any other clock methods.
See examples/advanced-features.md for countdown testing and session timeout patterns.
Pattern 9: ARIA Snapshot Testing (v1.49+)
Validate accessibility tree structure programmatically.
await expect(page.getByRole("navigation")).toMatchAriaSnapshot(`
- navigation:
- link "Home"
- link "Products"
- link "About"
`);
Why good: Catches ARIA issues before production, documents expected accessibility behavior
See examples/advanced-features.md for complex component ARIA snapshots.
</patterns>
<red_flags>
RED FLAGS
High Priority Issues:
- Using
page.waitForTimeout()with fixed delays -- causes flaky or slow tests, use auto-waiting assertions instead - Using CSS selectors like
.btn-primaryor#submit-btn-- fragile and break on refactoring, usegetByRole - Not testing error states -- only happy paths leaves error handling untested
- Tests sharing state or data -- causes flaky failures in parallel execution, isolate each test completely
Medium Priority Issues:
- Using
getByTestIdas primary locator -- misses accessibility validation, prioritizegetByRole - No network mocking for external APIs -- third-party flakiness affects your tests
- Running E2E tests only on one browser -- cross-browser issues go undetected
- Screenshots without masking dynamic content -- timestamps and ads cause false positives
Common Mistakes:
- Hardcoded test data scattered throughout files -- use named constants
- Testing implementation details (e.g., Redux state via
window.__REDUX_STATE__) instead of user behavior - Not using
beforeEachfor common setup -- leads to duplicated code - Mixing E2E tests with unit tests in the same directory
Gotchas & Edge Cases:
toBeVisible()auto-waits for the element;toBeAttached()checks DOM presence without visibility -- prefer visibility checks for most cases- Screenshots vary by OS and browser -- run visual tests in consistent CI environment
beforeAllruns once per worker, not once globally -- useglobalSetupin config for true one-time setup- Network routes are global to context -- routes set in
beforeEachoverride previous; always set up fresh per test - Parallel tests cannot share state -- use fixtures for per-test setup, not shared variables
toBeEditable()throws on non-editable elements (v1.50+) -- verify element type first- Glob URL patterns in
page.route()no longer support?and[](v1.52+) -- use regex instead route.continue()cannot override Cookie header (v1.52+) -- usecontext.addCookies()instead_reactand_vueselectors removed (v1.58) -- use data-testid or role-based locators
</red_flags>
<critical_reminders>
CRITICAL REMINDERS
All code must follow project conventions in CLAUDE.md
(You MUST use getByRole() as your primary locator strategy - it mirrors how users interact with the page)
(You MUST test complete user workflows end-to-end - login flows, checkout processes, form submissions)
(You MUST use web-first assertions that auto-wait - toBeVisible(), toHaveText(), not manual sleeps)
(You MUST isolate tests - each test runs independently with its own browser context)
(You MUST use named constants for test data - no magic strings or numbers in test files)
Failure to follow these rules will result in flaky tests, false positives, and maintenance nightmares.
</critical_reminders>