| name | e2e-tester |
| description | Playwright E2E testing patterns. Trigger: When writing Playwright E2E tests (Page Object Model, selectors, MCP exploration workflow).
|
| metadata | {"scope":["root","ui"],"auto_invoke":"Writing Playwright E2E tests"} |
| allowed-tools | Read, Edit, Write, Glob, Grep, Bash, WebFetch, WebSearch, Task |
Before you start:
- check if any existing tests cover the functionality you are testing
- check if a unit tests can cover the feature better
- analyze if a end to end test brings any value over a unit test
If the answer is yes to any of the above, proceed with the end to end test creation.
MCP Workflow (MANDATORY If Available)
⚠️ If you have Playwright MCP tools, ALWAYS use them BEFORE creating any test:
- Navigate to target page
- Take snapshot to see page structure and elements
- Interact with forms/elements to verify exact user flow
- Take screenshots to document expected states
- Verify page transitions through complete flow (loading, success, error)
- Document actual selectors from snapshots (use real refs and labels)
- Only after exploring create test code with verified selectors
If MCP NOT available: Proceed with test creation based on docs and code analysis.
Why This Matters:
- ✅ Precise tests - exact steps needed, no assumptions
- ✅ Accurate selectors - real DOM structure, not imagined
- ✅ Real flow validation - verify journey actually works
- ✅ Avoid over-engineering - minimal tests for what exists
- ✅ Prevent flaky tests - real exploration = stable tests
- ❌ Never assume how UI "should" work
Waiting Strategies (CRITICAL)
await page.waitForTimeout(2000);
await expect(element).toBeVisible();
await page.waitForResponse(resp => resp.url().includes('/api/data'));
await page.waitForURL('**/dashboard');
await expect(page.getByText('Success')).toBeVisible({ timeout: 10000 });
await page.goto('/app', { waitUntil: 'domcontentloaded' });
await expect(page.getByRole('main')).toBeVisible();
File Structure
tests/
├── base-page.ts # Parent class for ALL pages
├── helpers.ts # Shared utilities
└── {page-name}/
├── {page-name}-page.ts # Page Object Model
├── {page-name}.spec.ts # ALL tests here (NO separate files!)
└── {page-name}.md # Test documentation
File Naming:
- ✅
sign-up.spec.ts (all sign-up tests)
- ✅
sign-up-page.ts (page object)
- ✅
sign-up.md (documentation)
- ❌
sign-up-critical-path.spec.ts (WRONG - no separate files)
- ❌
sign-up-validation.spec.ts (WRONG)
Selector Priority (REQUIRED)
this.submitButton = page.getByRole("button", { name: "Submit" });
this.navLink = page.getByRole("link", { name: "Dashboard" });
this.emailInput = page.getByLabel("Email");
this.passwordInput = page.getByLabel("Password");
this.errorMessage = page.getByText("Invalid credentials");
this.pageTitle = page.getByText("Welcome");
this.customWidget = page.getByTestId("date-picker");
this.button = page.locator(".btn-primary");
this.input = page.locator("#email");
Scope Detection (ASK IF AMBIGUOUS)
| User Says | Action |
|---|
| "a test", "one test", "new test", "add test" | Create ONE test() in existing spec |
| "comprehensive tests", "all tests", "test suite", "generate tests" | Create full suite |
Examples:
- "Create a test for user sign-up" → ONE test only
- "Generate E2E tests for login page" → Full suite
- "Add a test to verify form validation" → ONE test to existing spec
Page Object Pattern
import { Page, Locator, expect } from "@playwright/test";
export class BasePage {
constructor(protected page: Page) {}
async goto(path: string): Promise<void> {
await this.page.goto(path);
await this.page.waitForLoadState("networkidle");
}
async waitForNotification(): Promise<void> {
await this.page.waitForSelector('[role="status"]');
}
async verifyNotificationMessage(message: string): Promise<void> {
const notification = this.page.locator('[role="status"]');
await expect(notification).toContainText(message);
}
}
export interface LoginData {
email: string;
password: string;
}
export class LoginPage extends BasePage {
readonly emailInput: Locator;
readonly passwordInput: Locator;
readonly submitButton: Locator;
constructor(page: Page) {
super(page);
this.emailInput = page.getByLabel("Email");
this.passwordInput = page.getByLabel("Password");
this.submitButton = page.getByRole("button", { name: "Sign in" });
}
async goto(): Promise<void> {
await super.goto("/login");
}
async login(data: LoginData): Promise<void> {
await this.emailInput.fill(data.email);
await this.passwordInput.fill(data.password);
await this.submitButton.click();
}
async verifyCriticalOutcome(): Promise<void> {
await expect(this.page).toHaveURL("/dashboard");
}
}
Page Object Reuse (CRITICAL)
Always check existing page objects before creating new ones!
import { SignInPage } from "../sign-in/sign-in-page";
import { HomePage } from "../home/home-page";
test("User can sign up and login", async ({ page }) => {
const signUpPage = new SignUpPage(page);
const signInPage = new SignInPage(page);
const homePage = new HomePage(page);
await signUpPage.signUp(userData);
await homePage.verifyPageLoaded();
await homePage.signOut();
await signInPage.login(credentials);
});
export class SignUpPage extends BasePage {
async logout() { }
async login() { }
}
Guidelines:
- Check
tests/ for existing page objects first
- Import and reuse existing pages
- Create page objects only when page doesn't exist
- If test requires multiple pages, ensure all page objects exist (create if needed)
Refactoring Guidelines
Move to BasePage when:
- ✅ Navigation helpers used by multiple pages (
waitForPageLoad(), getCurrentUrl())
- ✅ Common UI interactions (notifications, modals, theme toggles)
- ✅ Verification patterns repeated across pages (
isVisible(), waitForVisible())
- ✅ Error handling that applies to all pages
- ✅ Screenshot utilities for debugging
Move to helpers.ts when:
- ✅ Test data generation (
generateUniqueEmail(), generateTestUser())
- ✅ Setup/teardown utilities (
createTestUser(), cleanupTestData())
- ✅ Custom assertions (
expectNotificationToContain())
- ✅ API helpers for test setup (
seedDatabase(), resetState())
- ✅ Time utilities (
waitForCondition(), retryAction())
Before (BAD):
export class SignUpPage extends BasePage {
async waitForNotification(): Promise<void> {
await this.page.waitForSelector('[role="status"]');
}
}
export class SignInPage extends BasePage {
async waitForNotification(): Promise<void> {
await this.page.waitForSelector('[role="status"]');
}
}
After (GOOD):
export class BasePage {
async waitForNotification(): Promise<void> {
await this.page.waitForSelector('[role="status"]');
}
}
export function generateUniqueEmail(): string {
return `test.${Date.now()}@example.com`;
}
export function generateTestUser() {
return {
name: "Test User",
email: generateUniqueEmail(),
password: "TestPassword123!",
};
}
Test Pattern with Tags
import { test, expect } from "@playwright/test";
import { LoginPage } from "./login-page";
test.describe("Login", () => {
test("User can login successfully",
{ tag: ["@critical", "@e2e", "@login", "@LOGIN-E2E-001"] },
async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login({ email: "user@test.com", password: "pass123" });
await expect(page).toHaveURL("/dashboard");
}
);
});
Tag Categories:
- Priority:
@critical, @high, @medium, @low
- Type:
@e2e
- Feature:
@signup, @signin, @dashboard
- Test ID:
@SIGNUP-E2E-001, @LOGIN-E2E-002
Test Documentation Format ({page-name}.md)
### E2E Tests: {Feature Name}
**Suite ID:** `{SUITE-ID}`
**Feature:** {Feature description}
---
## Test Case: `{TEST-ID}` - {Test case title}
**Priority:** `{critical|high|medium|low}`
**Tags:**
- type → @e2e
- feature → @{feature-name}
**Description/Objective:** {Brief description}
**Preconditions:**
- {Prerequisites for test to run}
- {Required data or state}
### Flow Steps:
1. {Step 1}
2. {Step 2}
3. {Step 3}
### Expected Result:
- {Expected outcome 1}
- {Expected outcome 2}
### Key verification points:
- {Assertion 1}
- {Assertion 2}
### Notes:
- {Additional considerations}
Documentation Rules:
- ❌ NO general test running instructions
- ❌ NO file structure explanations
- ❌ NO code examples or tutorials
- ❌ NO troubleshooting sections
- ✅ Focus ONLY on specific test case
- ✅ Keep under 60 lines when possible
Authentication State Reuse
import { test as setup } from "@playwright/test";
setup("authenticate", async ({ page }) => {
await page.goto("/login");
await page.getByLabel("Email").fill("user@test.com");
await page.getByLabel("Password").fill("password");
await page.getByRole("button", { name: "Sign in" }).click();
await page.waitForURL("/dashboard");
await page.context().storageState({ path: ".auth/user.json" });
});
projects: [
{ name: "auth", testMatch: /auth\.setup\.ts/ },
{ name: "logged-in", dependencies: ["auth"], use: { storageState: ".auth/user.json" } },
{ name: "logged-out", use: { storageState: { cookies: [], origins: [] } } }
]
API Mocking
await page.route("**/api/users", route =>
route.fulfill({ json: { users: [{ id: 1, name: "Test" }] } })
);
await page.route("**/api/submit", route =>
route.fulfill({ status: 500, json: { error: "Server error" } })
);
await page.route("**/analytics/**", route => route.abort());
Test Fixtures
import { test as base } from "@playwright/test";
import { AdminPage } from "./admin/admin-page";
export const test = base.extend<{ adminPage: AdminPage }>({
adminPage: async ({ page }, use) => {
const admin = new AdminPage(page);
await admin.goto();
await use(admin);
},
});
test("admin can manage users", async ({ adminPage }) => {
await adminPage.deleteUser("test@example.com");
});
Parallel Execution
test.describe.configure({ mode: "serial" });
test("create user", async ({ page }) => {
const uniqueEmail = `user-${Date.now()}@test.com`;
});
Assertions
await expect.soft(page.getByText("Title")).toBeVisible();
await expect.soft(page.getByText("Subtitle")).toBeVisible();
await expect(async () => {
const count = await page.getByRole("listitem").count();
expect(count).toBeGreaterThan(5);
}).toPass({ timeout: 10000 });
await expect(page).toHaveScreenshot("dashboard.png");
await expect(page.getByRole("dialog")).toHaveScreenshot();
Common Pitfalls
const button = page.getByRole("button", { name: "Submit" });
await button.click();
await button.click();
await page.getByRole("button", { name: "Submit" }).click();
await page.getByRole("button", { name: "Confirm" }).click();
await element.click();
await element.click();
await expect(modal).toBeVisible();
const frame = page.frameLocator("#iframe-id");
await frame.getByRole("button").click();
await page.locator("custom-element").locator("internal:shadow=button").click();
Mobile & Viewport Testing
test.use({ viewport: { width: 375, height: 667 } });
import { devices } from "@playwright/test";
test.use({ ...devices["iPhone 13"] });
projects: [
{ name: "desktop", use: { viewport: { width: 1280, height: 720 } } },
{ name: "mobile", use: { ...devices["iPhone 13"] } },
]
Debugging & Traces
npx playwright test --trace on-first-retry
npx playwright show-trace trace.zip
npx playwright test --debug
npx playwright test --headed
use: {
trace: "on-first-retry",
screenshot: "only-on-failure",
video: "retain-on-failure",
}
CI/CD Configuration
export default defineConfig({
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: process.env.CI
? [["html"], ["github"], ["junit", { outputFile: "results.xml" }]]
: [["html"]],
use: {
trace: "on-first-retry",
screenshot: "only-on-failure",
},
});
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
Running Tests (PREFER PARTIAL RUNS)
Always prefer running specific tests over the entire suite for faster feedback:
npx playwright test
npx playwright test tests/login/login.spec.ts
npx playwright test tests/login/
npx playwright test --grep "login"
npx playwright test --grep "user can sign up"
npx playwright test --grep "@critical"
npx playwright test --grep "@LOGIN-E2E-001"
npx playwright test tests/login/login.spec.ts:42
npx playwright test --grep "login|signup"
npx playwright test --grep-invert "slow"
Other Commands
npx playwright test --debug
npx playwright test --headed
npx playwright test --trace on
npx playwright test --project=mobile
npx playwright codegen
Partial String Matching (CRITICAL for Robustness)
Always use partial/pattern matching instead of exact strings to reduce maintenance:
await expect(page.getByText("Welcome to Our Application!")).toBeVisible();
await expect(page.getByRole("button", { name: "Submit Form" })).toBeVisible();
await expect(notification).toHaveText("Your account has been created successfully.");
await expect(page.getByText(/welcome/i)).toBeVisible();
await expect(page.getByRole("button", { name: /submit/i })).toBeVisible();
await expect(notification).toContainText("account");
await expect(notification).toContainText(/created/i);
await expect(page.locator(".message")).toContainText("success");
await expect(page.getByText(/order #\d+/i)).toBeVisible();
await expect(page.getByText(/\d+ items? in cart/i)).toBeVisible();
Why Partial Matching:
- ✅ Survives minor copy/text changes
- ✅ Works across locales (case-insensitive)
- ✅ Less brittle to whitespace changes
- ✅ Easier to maintain long-term
- ❌ Exact matches break on punctuation, capitalization, or wording tweaks