Course contentsShow
← React
Lesson 2192 of 2,39648. End-to-End TestingFree lesson

What the Page Object Pattern Solves

Understanding why raw E2E tests become brittle and how Page Objects encapsulate DOM selectors and interactions.

What the Page Object Pattern Solves

What you'll learn: Why raw E2E tests become brittle and unmaintainable, and how Page Objects provide a cleaner solution.

The Brittleness Problem

When you write E2E tests directly against the DOM, you scatter selectors and interactions throughout your test files:

// Test 1
await page.click('[data-testid="login-email"]');
await page.fill('[data-testid="login-email"]', 'user@example.com');

// Test 2 (somewhere else)
await page.click('[data-testid="login-email"]');

// Test 3 (another file)
await page.locator('[data-testid="login-email"]').fill('admin@example.com');

The pain: When the UI changes—maybe that email field gets a new selector or the login flow adds a confirmation step—you must hunt through every test file and update dozens of duplicated selectors and interaction sequences.

The Page Object Solution

Think of Page Objects like a TV remote control. Instead of opening the TV and rewiring buttons every time the internal circuit board changes, the remote provides a stable interface: "turn on," "change channel," "adjust volume." The internal implementation can change, but the buttons stay the same.

A Page Object encapsulates all DOM selectors and interactions for a specific page or component:

class LoginPage {
  async fillEmail(email) {
    await page.fill('[data-testid="login-email"]', email);
  }
  
  async submitForm() {
    await page.click('button[type="submit"]');
  }
}

Now your tests call loginPage.fillEmail() instead of raw selectors. When the UI changes, you update one line in the Page Object, not fifty scattered test files.

Key Takeaway: Page Objects solve brittleness by encapsulating DOM selectors and interactions in a single, reusable abstraction—when the UI changes, you update one place instead of hunting through every test file.