When the "Submit" button gets renamed to "Sign In", a suite without Page Object Model (POM) needs every test that clicks it updated. With POM, you change one line in LoginPage.ts. A common convention keeps expect() assertions out of page object methods, so a failure points to the test that owns the check. Playwright's own page object example does put one inside a method, so treat the convention as a team choice.

Key takeaways

  • Locators and user-level actions such as login (not three separate clicks) live in the page class, so a renamed button costs one edit in one file.
  • Locators are lazy, so defining all of them in the constructor costs nothing until a method uses one.
  • A fixture built with base.extend passes page objects into the test signature, so test files need no new LoginPage(page) and no page-class imports.
  • Whether expect belongs inside a page object is a team convention, since Playwright's own example has one and many teams keep checks in tests. Pick one rule and apply it everywhere.
  • Keep inheritance to one level, from BasePage to a specific page.

What Page Object Model solves

POM is a design pattern, not a Playwright feature. A page object is a class that represents a page or a section of a page. It holds the locators and the methods that interact with that page, and tests call it instead of raw Playwright commands.

Without POM, your tests look like this:

test('user can log in', async ({ page }) => {
  await page.goto('https://lab.becomeqa.com');
  await page.getByRole('button', { name: 'Login' }).click();
  await page.getByLabel('Username').fill('admin@becomeqa.com');
  await page.getByLabel('Password').fill('testpass123');
  await page.getByRole('button', { name: 'Submit' }).click();
  await expect(page.getByText('My Travel Items')).toBeVisible();
});

test('login fails with wrong password', async ({ page }) => {
  await page.goto('https://lab.becomeqa.com');
  await page.getByRole('button', { name: 'Login' }).click();
  await page.getByLabel('Username').fill('admin@becomeqa.com');
  await page.getByLabel('Password').fill('wrongpassword');
  await page.getByRole('button', { name: 'Submit' }).click();
  await expect(page.getByText('Invalid credentials')).toBeVisible();
});

With POM:

test('user can log in', async ({ page }) => {
  const loginPage = new LoginPage(page);
  await loginPage.goto();
  await loginPage.login('admin@becomeqa.com', 'testpass123');
  await expect(page.getByText('My Travel Items')).toBeVisible();
});

test('login fails with wrong password', async ({ page }) => {
  const loginPage = new LoginPage(page);
  await loginPage.goto();
  await loginPage.login('admin@becomeqa.com', 'wrongpassword');
  await expect(page.getByText('Invalid credentials')).toBeVisible();
});

If the button is renamed from "Submit" to "Sign In", only LoginPage.ts changes and both tests stay as they are.

Building your first Page Object

Create a pages/ folder at the project root. Inside it, LoginPage.ts:

import { Page, Locator } from '@playwright/test';

export class LoginPage {
  readonly page: Page;
  readonly loginButton: Locator;
  readonly usernameInput: Locator;
  readonly passwordInput: Locator;
  readonly submitButton: Locator;

  constructor(page: Page) {
    this.page = page;
    this.loginButton = page.getByRole('button', { name: 'Login' });
    this.usernameInput = page.getByLabel('Username');
    this.passwordInput = page.getByLabel('Password');
    this.submitButton = page.getByRole('button', { name: 'Submit' });
  }

  async goto() {
    await this.page.goto('https://lab.becomeqa.com');
  }

  async login(username: string, password: string) {
    await this.loginButton.click();
    await this.usernameInput.fill(username);
    await this.passwordInput.fill(password);
    await this.submitButton.click();
  }
}

The Playwright Locators: Which One to Use and When as class properties, the way Playwright's page object example does it. They are lazy, so they don't search the DOM when defined, only when used. The methods are user actions (goto, login) that bundle several clicks, so tests call actions and never touch implementation details.

Add more pages

Add a DashboardPage.ts for the page after login:

import { Page, Locator } from '@playwright/test';

export class DashboardPage {
  readonly page: Page;
  readonly heading: Locator;
  readonly itemsTable: Locator;
  readonly addItemButton: Locator;

  constructor(page: Page) {
    this.page = page;
    this.heading = page.getByRole('heading', { name: 'My Travel Items' });
    this.itemsTable = page.getByRole('table');
    this.addItemButton = page.getByRole('button', { name: 'Add Item' });
  }

  async isLoaded() {
    await this.heading.waitFor({ state: 'visible' });
  }

  async getRowCount() {
    const rows = this.page.getByRole('row');
    return await rows.count() - 1; // subtract header row
  }

  async clickAddItem() {
    await this.addItemButton.click();
  }
}

Tests now read like a user flow:

import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
import { DashboardPage } from '../pages/DashboardPage';

test('dashboard shows items after login', async ({ page }) => {
  const loginPage = new LoginPage(page);
  const dashboardPage = new DashboardPage(page);

  await loginPage.goto();
  await loginPage.login('admin@becomeqa.com', 'testpass123');
  await dashboardPage.isLoaded();

  const rowCount = await dashboardPage.getRowCount();
  expect(rowCount).toBeGreaterThan(0);
});

Use a BasePage for shared behavior

If several page objects need the same helpers, put them in a base class:

// pages/BasePage.ts
import { Page } from '@playwright/test';

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

  async getPageTitle() {
    return await this.page.title();
  }

  async scrollToBottom() {
    await this.page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
  }
}
// pages/LoginPage.ts
import { Locator } from '@playwright/test';
import { BasePage } from './BasePage';

export class LoginPage extends BasePage {
  readonly loginButton: Locator;
  readonly usernameInput: Locator;
  readonly passwordInput: Locator;
  readonly submitButton: Locator;

  constructor(page: any) {
    super(page);
    this.loginButton = page.getByRole('button', { name: 'Login' });
    this.usernameInput = page.getByLabel('Username');
    this.passwordInput = page.getByLabel('Password');
    this.submitButton = page.getByRole('button', { name: 'Submit' });
  }

  async goto() {
    await this.page.goto('https://lab.becomeqa.com');
  }

  async login(username: string, password: string) {
    await this.loginButton.click();
    await this.usernameInput.fill(username);
    await this.passwordInput.fill(password);
    await this.submitButton.click();
  }
}

One level of inheritance, from BasePage to a specific page, is usually enough, since deep chains become hard to follow.

Turn page objects into fixtures

Creating new LoginPage(page) in every test is repetitive. Custom Fixtures in Playwright: The Pattern That Makes Tests Read Like Prose let you inject page objects automatically:

// fixtures/pages.fixture.ts
import { test as base } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
import { DashboardPage } from '../pages/DashboardPage';

type PageFixtures = {
  loginPage: LoginPage;
  dashboardPage: DashboardPage;
};

export const test = base.extend<PageFixtures>({
  loginPage: async ({ page }, use) => {
    await use(new LoginPage(page));
  },
  dashboardPage: async ({ page }, use) => {
    await use(new DashboardPage(page));
  },
});

export { expect } from '@playwright/test';

Now import test from your fixture file instead of from Playwright:

// tests/login.spec.ts
import { test, expect } from '../fixtures/pages.fixture';

test('user can log in', async ({ loginPage }) => {
  await loginPage.goto();
  await loginPage.login('admin@becomeqa.com', 'testpass123');
  await expect(loginPage.page.getByText('My Travel Items')).toBeVisible();
});

test('dashboard shows items after login', async ({ loginPage, dashboardPage }) => {
  await loginPage.goto();
  await loginPage.login('admin@becomeqa.com', 'testpass123');
  await dashboardPage.isLoaded();

  const rowCount = await dashboardPage.getRowCount();
  expect(rowCount).toBeGreaterThan(0);
});

Page objects arrive through the test signature, so test files need no constructor calls and no imports of page classes.

What belongs in a page object and what doesn't

Page objects hold

  • Locators for the page's elements
  • Methods that represent user actions on the page
  • Wait conditions specific to the page (isLoaded, waitForModal)
  • Simple data getters (getRowCount, getHeadingText)

Test files keep

  • Assertions (expect(...))
  • Test data
  • Test logic (what to do, in what order)
  • Descriptions of what the test verifies

A common rule is that page objects describe how to interact with a page, and tests describe what to verify. An assertion inside a page object method hides the check from the test, and a failure points to a line in the page class.

// Wrong — assertion inside page object
async login(username: string, password: string) {
  await this.loginButton.click();
  await this.usernameInput.fill(username);
  await this.passwordInput.fill(password);
  await this.submitButton.click();
  await expect(this.page.getByText('My Travel Items')).toBeVisible(); // don't do this
}

// Right — page object just performs the action
async login(username: string, password: string) {
  await this.loginButton.click();
  await this.usernameInput.fill(username);
  await this.passwordInput.fill(password);
  await this.submitButton.click();
}
// Test does the assertion
await loginPage.login('admin@becomeqa.com', 'testpass123');
await expect(page.getByText('My Travel Items')).toBeVisible();

Project structure with POM

A folder structure for a POM-based project:

project/
  pages/
    BasePage.ts
    LoginPage.ts
    DashboardPage.ts
    ItemsPage.ts
  fixtures/
    pages.fixture.ts
    auth.fixture.ts
  tests/
    auth/
      login.spec.ts
      logout.spec.ts
    items/
      items-list.spec.ts
      items-crud.spec.ts
    api/
      items-api.spec.ts
  playwright.config.ts
  package.json
Create a page object once two or more tests use the same page. For a single test, a class costs more than it saves.

FAQ

How do I wait for a page to be ready inside a page object?

Wait on an element or a web assertion, the way isLoaded does with heading.waitFor(). Avoid networkidle, which Playwright discourages in its page.goto and waitForLoadState docs.

When is a page object not worth it?

When no test uses the page, or only one does. A settings page without tests needs no SettingsPage.ts, and the class pays off once a second test repeats the same locators and steps.

What do I do when a page object grows too large?

Split it by section. A page with a table, a modal and a sidebar can become ItemsTablePage, AddItemModal and a parent ItemsPage that composes them. If LoginPage extends a BasePage with protected page, a test cannot read loginPage.page, and the fixture example reads it, so that example works only with the public readonly page version.

→ See also: Custom Fixtures in Playwright: The Pattern That Makes Tests Read Like Prose | Advanced Page Object Model Patterns in Playwright | TypeScript Interfaces and Types for Page Object Model | JavaScript Classes for QA Engineers: Building Page Objects