Playwright Page Object Model: A Simple Guide to Writing Maintainable Tests

 


Imagine you have 100 automated tests.

Now imagine the login button changes its HTML.

If the login locator is written separately inside 40 tests, you may have to fix 40 files.

That is one of the problems the Playwright Page Object Model helps solve.

Instead of mixing page details with test logic, you can organize commonly used page actions into reusable classes.

The result is simpler tests, easier maintenance, and a cleaner automation framework.

What Is Page Object Model?

The Page Object Model, often called POM, is a design pattern used to organize automated tests.

The basic idea is simple:

Keep page-related actions in one place and keep test scenarios focused on what you want to verify.

For example, a login page may contain:

  • Username field

  • Password field

  • Login button

  • Error message

  • Dashboard navigation

Instead of writing all these locators repeatedly, you can create a LoginPage class.

The test can then say:

loginPage.login(username, password)


The test becomes easier to understand because it describes the user's action rather than every technical detail.

Why Use POM With Playwright?

Small Playwright tests can be written without POM.

But as a project grows, repeated code becomes harder to maintain.

A good POM structure can help with:

  • Reusing locators

  • Reusing common actions

  • Reducing duplicate code

  • Keeping tests readable

  • Updating selectors in one place

  • Creating a consistent automation framework

The official Playwright documentation also describes page objects as a way to structure larger test suites and keep selectors and reusable code organized.

A Simple Example

Suppose your application has a login page.

Without a page object, a test might contain everything:

await page.getByLabel('Username').fill('user1');

await page.getByLabel('Password').fill('password123');

await page.getByRole('button', { name: 'Login' }).click();


await expect(page.getByText('Dashboard')).toBeVisible();


This works.

But if many tests need to log in, the same actions may appear repeatedly.

A page object can move those actions into a reusable method:

async login(username, password) {

    await this.username.fill(username);

    await this.password.fill(password);

    await this.loginButton.click();

}


Then your test can focus on the scenario:

await loginPage.login('user1', 'password123');

await expect(page.getByText('Dashboard')).toBeVisible();


The difference may look small in one test.

It becomes much more important when your project contains hundreds of tests.

How Should You Design a Playwright Page Object?

A simple page object usually contains:

  1. The Playwright Page

  2. Locators

  3. Page actions

  4. Reusable workflows

For example:

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


export class LoginPage {

    constructor(page) {

        this.page = page;


        this.username = page.getByLabel('Username');

        this.password = page.getByLabel('Password');

        this.loginButton = page.getByRole('button', { name: 'Login' });

    }


    async login(username, password) {

        await this.username.fill(username);

        await this.password.fill(password);

        await this.loginButton.click();

    }

}


Now the test does not need to know how the login form is implemented.

It only needs to know that a login action exists.

POM Does Not Mean "Put Everything in One Class"

This is a common mistake.

A huge page object containing hundreds of unrelated methods can become difficult to maintain.

Instead, keep responsibilities clear.

For example:

pages/

    LoginPage.js

    HomePage.js

    ProductPage.js

    CheckoutPage.js


Each class represents a meaningful part of the application.

This makes the framework easier to navigate.

POM and Locators Should Work Together

A page object is only as stable as the locators inside it.

Prefer meaningful locators that describe the user's view of the application.

For example:

page.getByRole('button', { name: 'Submit' })


can be easier to understand than a long CSS selector.

Good locator design is important because UI changes are common.

If a selector depends heavily on implementation details, small frontend changes can break tests unnecessarily.

POM Should Represent User Actions

Think about what a real user does.

A user does not think:

Find element number 27 and click it.

The user thinks:

Log in.

So your automation should ideally express the same idea.

For example:

await loginPage.login('user1', 'password123');


is easier to understand than several low-level commands repeated throughout the test suite.

POM With Assertions

Another important design decision is deciding where assertions belong.

A practical approach is to keep business-level verification in tests when it makes the scenario clearer.

For example:

await loginPage.login(username, password);


await expect(page.getByRole('heading', {

    name: 'Dashboard'

})).toBeVisible();


The page object performs the action.

The test explains what outcome is expected.

This separation can make the test easier to read.

Common POM Mistakes

Mistake 1: Duplicate locators

Do not define the same locator in many files without a reason.

Mistake 2: Giant page classes

Do not create one class containing the entire application.

Mistake 3: Hard-coded waits

Avoid using fixed delays just because a page sometimes loads slowly.

Investigate the real synchronization problem instead.

Mistake 4: Mixing unrelated business logic

A page object should not become a dumping ground for every utility function.

Mistake 5: Making tests unreadable

If abstraction makes the test harder to understand, the abstraction may be too complicated.

When Should Beginners Learn POM?

You do not need to master POM before writing your first Playwright test.

A simple learning order is:

Browser basics → Locators → Actions → Assertions → Test structure → POM → Fixtures → API testing → CI/CD

First understand how Playwright works.

Then learn how to organize a growing test suite.

That approach is easier than trying to learn an entire framework architecture on day one.

A Simple Real-World Structure

A Playwright project could eventually look like:

playwright-project/

│

├── tests/

│   ├── login.spec.js

│   ├── checkout.spec.js

│   └── search.spec.js

│

├── pages/

│   ├── LoginPage.js

│   ├── HomePage.js

│   └── CheckoutPage.js

│

├── fixtures/

│

├── utils/

│

├── playwright.config.js

└── package.json


The exact structure can change depending on project size.

The goal is not to create the most complicated folder structure.

The goal is to make the project easy for another tester to understand.

Final Takeaway

The Playwright Page Object Model is not just about creating classes.

It is about controlling complexity.

When an automation project grows, repeated selectors and actions can become difficult to manage.

POM gives your team a practical way to separate page behavior from test scenarios.

Start small.

Create page objects for important application areas.

Keep locators meaningful.

Keep actions reusable.

Keep tests focused on behavior.

That is how a collection of scripts can gradually become a maintainable Playwright automation framework.

If you are learning Playwright from the beginning, focus first on understanding how the browser, locators, actions, assertions, and test runner work. Then introduce POM when your tests begin to repeat.


Comments

Popular posts from this blog

How to Run Only Smoke Tests in Playwright: Step-by-Step Guide

How to Test REST APIs Using Playwright

Playwright Multiple Tabs and Windows Explained: A Practical Guide