Playwright + TS · Lesson 24 of 36

0/36 done

Module 24: Page Object Model (POM)

24. Enterprise Page Object Model & Component Architecture

22 minlesson

Try it yourself

Implement CartComponent:

  1. Declare a private field itemCount initialized to 0.
  2. Implement public method addItem() that increments itemCount.
  3. Implement public method getItemCount() that returns itemCount.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Why Page Object Model (POM)?

Without POM, when a login button selector changes, you must update 50 different test files. With POM:

  • Single Source of Truth: Locators and UI actions live in dedicated class files.
  • Readable Tests: Tests read like plain-English business workflows.
  • Maintainability: UI changes require updating only one method or locator.

Professional POM Architecture

typescript
// pages/BasePage.ts
import { Page, Locator } from "@playwright/test";

export abstract class BasePage {
  constructor(protected readonly page: Page) {}

  public async navigateTo(path: string): Promise<void> {
    await this.page.goto(path);
  }

  public async waitForPageLoad(): Promise<void> {
    await this.page.waitForLoadState("domcontentloaded");
  }
}
typescript
// pages/LoginPage.ts
import { Page, Locator, expect } from "@playwright/test";
import { BasePage } from "./BasePage";

export class LoginPage extends BasePage {
  // 1. Declare Locators as readonly properties
  private readonly usernameInput: Locator;
  private readonly passwordInput: Locator;
  private readonly loginButton: Locator;
  private readonly errorMessage: Locator;

  constructor(page: Page) {
    super(page);
    this.usernameInput = page.getByLabel("Username");
    this.passwordInput = page.getByLabel("Password");
    this.loginButton = page.getByRole("button", { name: "Sign In" });
    this.errorMessage = page.getByRole("alert");
  }

  // 2. High-level business actions
  public async login(user: string, pass: string): Promise<void> {
    await this.usernameInput.fill(user);
    await this.passwordInput.fill(pass);
    await this.loginButton.click();
  }

  // 3. Status checks
  public async expectErrorMessage(message: string): Promise<void> {
    await expect(this.errorMessage).toContainText(message);
  }
}

Good POM vs Bad POM

  • Good POM: Exposes user-intent methods (login(user, pass), addItemToCart(item)). Locators are private.
  • Bad POM: Exposes raw locators or exposes tiny methods for every single click (clickUsername(), clickPassword()).

Key Takeaways & Walkthrough Summary

Welcome to Phase 4! We begin with the Page Object Model, the gold standard design pattern for maintainable automation. In this lesson, you will learn how to build strongly-typed BasePage classes, encapsulate locators as private properties, compose reusable Component Objects, and separate user actions from assertions. Let's architect clean page objects!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Should Page Objects contain assertions?

It is a best practice to keep tests responsible for assertions (e.g. expect(dashboard.isLoaded()).toBe(true)), or provide specialized assertion helper methods on the POM (e.g. expectErrorMessage). Avoid mixing business assertions inside navigation methods.

Can Page Objects inherit from each other?

Yes! BasePage holds common elements like header, footer, and navigation methods, which child pages inherit using TypeScript 'extends'.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip