Back To Blogs

Page Object Model in Test Automation: When It Helps and When It Hurts

The Page Object Model (POM) is the most widely used design pattern in UI test automation: each page or component gets a class that knows how to interact with it, so tests can focus on what they check. Used well, it removes duplication and makes tests easy to read. Used badly, it creates a second, harder-to-maintain codebase.

The basic idea

Without page objects, locators and steps are repeated across tests:

test('apply coupon', async ({ page }) => {
  await page.goto('/cart');
  await page.getByLabel('Coupon code').fill('WELCOME10');
  await page.getByRole('button', { name: 'Apply' }).click();
  await expect(page.getByTestId('cart-total')).toHaveText('$27.00');
});

With a page object:

// pages/cart-page.ts
import { type Page, type Locator } from '@playwright/test';

export class CartPage {
  readonly total: Locator;
  constructor(private readonly page: Page) {
    this.total = page.getByTestId('cart-total');
  }
  async open() {
    await this.page.goto('/cart');
  }
  async applyCoupon(code: string) {
    await this.page.getByLabel('Coupon code').fill(code);
    await this.page.getByRole('button', { name: 'Apply' }).click();
  }
  async remove(productName: string) {
    await this.page
      .getByRole('listitem')
      .filter({ hasText: productName })
      .getByRole('button', { name: 'Remove' })
      .click();
  }
}
// tests/cart.spec.ts
test('a coupon cannot be applied twice', async ({ page }) => {
  const cart = new CartPage(page);
  await cart.open();
  await cart.applyCoupon('WELCOME10');
  await cart.applyCoupon('WELCOME10');
  await expect(cart.total).toHaveText('$27.00');
  await expect(page.getByRole('alert')).toContainText('already applied');
});

When the coupon field's label changes, you update one line, not twenty tests.

Guidelines that keep it healthy

  1. Model user intent, not every element. applyCoupon(code) is better than exposing couponInput and applyButton separately.
  2. Keep assertions in tests. Page objects describe how to interact; tests decide what's correct. (Exposing locators like total for tests to assert on is fine.)
  3. Return data or the next page, not booleans. Avoid methods like isCouponApplied(), which hide what's being checked.
  4. Compose components. A Header or ProductCard component object can be reused across pages.
  5. Avoid deep inheritance. BasePage → ShopPage → CartPage → DiscountCartPage becomes a maze. Prefer composition.

Common failure modes

  • God objects: a 1,500-line page class nobody wants to touch. Split by component.
  • Mirroring the DOM: one method per element turns page objects into a locator dictionary with extra steps.
  • Hidden waits and retries inside page methods mask real timing problems.
  • Premature abstraction: creating page objects before you have three tests that need them.

Lighter alternatives

You don't always need classes:

  • Helper functions: await addToCart(page, 'Ceramic mug'). Simple, and often enough for small suites.
  • Fixtures (Playwright): inject ready-made page objects or pre-signed-in pages into tests:
// fixtures.ts
import { test as base } from '@playwright/test';
import { CartPage } from './pages/cart-page';

export const test = base.extend<{ cart: CartPage }>({
  cart: async ({ page }, use) => {
    const cart = new CartPage(page);
    await cart.open();
    await use(cart);
  },
});
  • Screenplay pattern: actors performing tasks and asking questions. It's more structure, and it pays off in very large suites with many user roles.

How to decide

Suite sizeSuggested approach
A handful of testsPlain tests, maybe a few helpers
Dozens of tests across several pagesPage or component objects + fixtures
Hundreds of tests, many roles and teamsComponent objects, fixtures, possibly Screenplay

Refactor toward patterns when duplication hurts, not in anticipation of it.

Test design comes first

Patterns make tests maintainable, but they don't decide what to test. Start from risks and real bugs: explore an app in QAUnlocked Practice Labs, list the defects you found, and design page objects around the flows that matter, like cart, coupons and checkout.

page object modeltest automationdesign patternsplaywright

Comments

0 Comments

Be the first to comment.