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

Testing with Page Objects: Before and After

Comparing raw E2E test code versus tests using Page Objects to see the readability and maintenance improvements.

Testing with Page Objects: Before and After

You'll learn how Page Objects dramatically improve test readability and maintenance by comparing raw E2E tests against refactored versions.

The Raw Test Problem

Without Page Objects, E2E tests become verbose tangles of locators and assertions scattered throughout. Every test duplicates the same selectors, making them fragile—when a button's data-testid changes, you hunt through dozens of test files updating the same locator everywhere.

Here's a typical "before" test:

test('user can log in', async ({ page }) => {
  await page.goto('/login');
  await page.locator('[data-testid="email-input"]').fill('user@example.com');
  await page.locator('[data-testid="password-input"]').fill('password123');
  await page.locator('[data-testid="login-button"]').click();
  await expect(page.locator('[data-testid="dashboard-header"]')).toBeVisible();
});

The Page Object Transformation

Now with Page Objects:

test('user can log in', async ({ page }) => {
  const loginPage = new LoginPage(page);
  await loginPage.goto();
  await loginPage.login('user@example.com', 'password123');
  await expect(loginPage.dashboardHeader).toBeVisible();
});

Notice the difference? The test reads like plain English describing user behavior. The messy locator details hide inside LoginPage, where they belong. If that email input's selector changes, you fix it in one place—the Page Object—and every test instantly works again.

The Maintenance Win

Think of it like refactoring repeated code into functions. Raw tests are like copying the same 20 lines everywhere; Page Objects are like calling a well-named method. When requirements change (and they always do), you update the Page Object once instead of updating 50 tests.

Key Takeaway: Page Objects transform unreadable, brittle E2E tests into maintainable, self-documenting code by centralizing locators and actions—update once, benefit everywhere.