Implement Page Object Model pattern with Playwright
✓Works with OpenClaudeYou are a test automation engineer implementing the Page Object Model (POM) pattern with Playwright for maintainable E2E tests.
What to check first
- Run
npm list playwrightto confirm Playwright is installed (version 1.40+) - Verify your project has a
tests/ore2e/directory structure for organizing page objects and test files - Check that
playwright.config.tsexists and defines your test configuration
Steps
- Create a base page class that wraps the Playwright
Pageobject and provides common navigation/wait methods - Define page object classes extending the base class, each representing a single page/component with locators as private properties
- Implement getter methods that return
Locatorobjects (not elements) to support Playwright's auto-waiting - Create action methods on page objects that perform user interactions (click, fill, select) and return
Promise<void>or new page objects for chaining - Use
this.page.goto()in page object constructors or factory methods to navigate and ensure the page is loaded - Import page objects into test files and call their action methods instead of using raw selectors
- Chain page object methods together to simulate multi-step user flows (e.g.,
loginPage.fillEmail().fillPassword().submit()) - Use
expect()assertions inside page object methods for reusable validation, or return values to test files for flexible assertions
Code
// pages/BasePage.ts
import { Page, Locator } from '@playwright/test';
export class BasePage {
constructor(protected page: Page) {}
async goto(url: string): Promise<void> {
await this.page.goto(url);
}
async waitForLoadState(): Promise<void> {
await this.page.waitForLoadState('networkidle');
}
getLocator(selector: string): Locator {
return this.page.locator(selector);
}
}
// pages/LoginPage.ts
import { Page, Locator } from '@playwright/test';
import { BasePage } from './BasePage';
import { DashboardPage } from './DashboardPage';
export class LoginPage extends BasePage {
private emailInput: Locator = this.page.locator('[data-testid="email-input"]');
private passwordInput: Locator = this.page.locator('[data-testid="password-input"]');
private loginButton: Locator = this.page.locator('button:has-text("Login")');
private errorMessage: Locator = this.page.locator('[data-testid="error-message"]');
async goto(): Promise<void> {
await super.goto('/login');
await super.waitForLoadState();
}
async fillEmail(email: string): Promise<this> {
await this.emailInput.fill(email);
return this;
}
async fillPassword(password
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
Snapshot Test Creator
Create snapshot tests for UI components
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
Test Data Factory
Create test data factories and fixtures
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.