Skip to content
Napatdev
HomeProjectsNotesAI SkillsAboutContact
Menu
Napatdev
HomeProjectsNotesAI SkillsAboutContact

LanguageENTH

Get in touch

[email protected]
TH
All notes
ENTH

Playwright Page Object Model: a test structure that stays maintainable

Written byNapat Pamornsut·~1 min read·Published Sep 14, 2026
On this page
  • Environment
  • The problem POM solves
  • Recommended project layout
  • Real example: contact page
  • Three rules of a good POM
  • Common mistakes
  • References
  • Related notes

Short answer: As suites grow, selectors scatter across files and one UI change breaks ten tests. The Page Object Model (POM) fixes this by collecting each page's elements and actions in a single class, so test files read like human language.

Environment

  • Playwright 1.x + TypeScript
  • Read What is Playwright? first for the basics

The problem POM solves

Unstructured tests (one change, ten broken files):

ts
// tests/contact-a.spec.ts
await page.getByLabel('Email').fill('[email protected]');
await page.getByRole('button', { name: 'Send message' }).click();

// tests/contact-b.spec.ts — same selectors, copy-pasted
await page.getByLabel('Email').fill('[email protected]');
await page.getByRole('button', { name: 'Send message' }).click();

Rename the email label and you must chase every copy. POM keeps it in one place — one fix, done.

Recommended project layout

text
tests/
  pages/
    contact.page.ts      # one page object per page
    notes.page.ts
  fixtures/
    test.ts              # extended base test wiring page objects
  contact.spec.ts        # test files consume fixtures
  notes.spec.ts

Real example: contact page

tests/pages/contact.page.ts:

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

export class ContactPage {
  readonly page: Page;
  readonly emailInput: Locator;
  readonly messageInput: Locator;
  readonly sendButton: Locator;
  readonly successMessage: Locator;

  constructor(page: Page) {
    this.page = page;
    this.emailInput = page.getByLabel('Email');
    this.messageInput = page.getByLabel('Message');
    this.sendButton = page.getByRole('button', { name: 'Send message' });
    this.successMessage = page.getByText('Message sent');
  }

  async goto() {
    await this.page.goto('/contact');
  }

  async submit(email: string, message: string) {
    await this.emailInput.fill(email);
    await this.messageInput.fill(message);
    await this.sendButton.click();
  }
}

tests/fixtures/test.ts — wire the page object into the test context:

ts
import { test as base } from '@playwright/test';
import { ContactPage } from '../pages/contact.page';

type Fixtures = {
  contactPage: ContactPage;
};

export const test = base.extend<Fixtures>({
  contactPage: async ({ page }, use) => {
    await use(new ContactPage(page));
  },
});
export { expect } from '@playwright/test';

tests/contact.spec.ts — test files read like prose:

ts
import { test, expect } from './fixtures/test';

test('contact message succeeds', async ({ contactPage }) => {
  await contactPage.goto();
  await contactPage.submit('[email protected]', 'Hello');

  await expect(contactPage.successMessage).toBeVisible();
});

Three rules of a good POM

  1. Locators live in page objects only — a getBy* in a test file means a leak.
  2. Name methods after user actions — submit(), searchFor(), not clickButton(), so a button-to-link redesign never touches test files.
  3. No expect in page objects — assertions belong to test files; page objects act, they do not judge.

Common mistakes

  1. God-class page objects — pages with many sections deserve components (e.g. SearchBar, Pagination) composed together.
  2. Cross-test state in page objects — construct them fresh per test via fixtures; never cache data between tests.
  3. POM from day one with 3 tests — over-engineering. Write plainly first; extract a page object when a selector repeats in a third file.
  4. Test data mixed with locators — test emails and messages should be method parameters, not class constants.

References

  • Playwright documentation: https://playwright.dev/docs/api/class-test

Related notes

  • What is Playwright? Start E2E testing with TypeScript for real projects
  • All testing notes

Author: Napat Pamornsut — About · Projects · All notes

← Previous noteOdoo QWeb Table Border: controlling PDF table lines on every page
Next note →What is Playwright? Start E2E testing with TypeScript for real projects
N

Napat Pamornsut

Web Developer / Software Tester

AboutProjectsAll notes

Related projects

  • Shop Inventory & Sales Management System
  • Automated Testing for Clean Water Monitoring
  • UAT / Test Case & Bug Report Template

On this page

  • Environment
  • The problem POM solves
  • Recommended project layout
  • Real example: contact page
  • Three rules of a good POM
  • Common mistakes
  • References
  • Related notes

All notes

  • Next.js Server Actions explained: when to use them vs Route Handlers
  • Next.js Server Actions Security: validation, authorization and CSRF
  • Odoo 19 Certification study guide: exam structure and reading plan
  • Odoo Automated Action: why imports fail and how to fix IMPORTNAME
  • Odoo Automated Action: Fix forbidden opcode STOREATTR with record.write
  • Odoo QWeb: fixing PDF page breaks with real examples
  • Odoo QWeb Table Border: controlling PDF table lines on every page
  • Playwright Page Object Model: a test structure that stays maintainable
  • What is Playwright? Start E2E testing with TypeScript for real projects
  • Next.js Server Actions + Prisma Transaction: atomic mutations

Napat Pamornsut

ณภัทร ภมรสูตร

Web development & software testing

ProjectsNotesAI SkillsAboutContactSearch

© 2026 Napatdev

Back to top