Page Object Model Pattern
You'll learn how to organize E2E tests using page objects — classes that encapsulate locators and actions — making tests cleaner, more maintainable, and easier to update when UI changes.
The Core Problem
As your Playwright tests grow, you'll notice duplication: the same locators (page.getByRole('button', { name: 'Submit' })) and action sequences appear across multiple test files. When a button's text changes from "Submit" to "Send," you'll need to update dozens of tests. This is brittle and time-consuming.
What Page Objects Are
The Page Object Model (POM) is a design pattern where you create classes (or plain objects) that represent pages or components in your app. Each page object encapsulates:
- Locators for elements on that page
- Actions users can perform
- Assertions about page state
Think of it like creating a remote control for each page: instead of test code directly manipulating TV internals, it presses clearly-labeled buttons on the remote.
The Pattern in Practice
// pages/LoginPage.ts
export class LoginPage {
constructor(private page) {}
// Locators
get emailInput() { return this.page.getByLabel('Email') }
get passwordInput() { return this.page.getByLabel('Password') }
get submitButton() { return this.page.getByRole('button', { name: 'Log In' }) }
// Actions
async login(email: string, password: string) {
await this.emailInput.fill(email)
await this.passwordInput.fill(password)
await this.submitButton.click()
}
}
// In your test
const loginPage = new LoginPage(page)
await loginPage.login('user@test.com', 'password123')
Now when the login button text changes, you update one line in LoginPage.ts, not fifty tests.
Key Takeaway: Page objects act as an abstraction layer between tests and UI implementation, centralizing element locators and user actions to dramatically reduce maintenance burden when your app evolves.