What problem Page Object solves
A Page Object moves a screen's locators and actions into one class, so a layout change needs a fix in one place. The idea is to separate the reasons for change: the scenario changes with product behaviour, locators with the layout. For few tests a helper function may be enough, and a class per URL is not the goal: the boundary is a logical screen, a form or a modal.
Patterns · Page Object
The button «Sign in» was renamed to «Log in» and 40 tests failed. How many lines to fix?
A designer renamed the login button. Every test that finds it by role and name went red: 40 files, each with the same three lines of fill and click. One change, 40 places to touch, and the next rename will repeat it. This lesson explains what problem Page Object solves, why it is built this way and where its limits are.
How it works and why. A Page Object (POM) is a class that describes a screen in the user's language. It holds locators ("where the elements are") and actions ("what is done with them"). The test says what to do ("log in") and the class knows how. The idea is to separate the reasons for change: a business scenario changes when product behaviour changes, while locators and the order of clicks change when the markup changes. Markup changes far more often. If both layers live in one place, every UI edit touches every test. If they are separated, a markup edit touches one file.
Where the knowledge about a page lives
This is especially convenient in Playwright because a Locator is lazy: it describes how to find an element but searches for nothing at the moment of creation. Search and waiting happen only on an action or an assertion. So it is safe to create locators in the constructor even when the page is not open yet, and a Page Object needs no "initialisation" before use.
const loginPage = new LoginPage(page);
// page is still about:blank, yet no error: the Locator searched for nothing
await loginPage.goto();
await loginPage.submit.click(); // it searches and waits here
Example: from 40 copies to one line. The starting point: every test knows by itself how the form works.
test('login: success', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('qa@test.com');
await page.getByLabel('Password').fill('secret');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page).toHaveURL('/dashboard');
});
test('login: wrong password', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('qa@test.com');
await page.getByLabel('Password').fill('wrong');
await page.getByRole('button', { name: 'Sign in' }).click();
// 38 more files with the same three lines
});
Each test reads fine on its own. The problem is scale: the knowledge "the button is called Sign in" is copied to every file. After the rename each of them fails with a timeout because the locator found nothing, and the cause is the same in all forty reports. Now the same knowledge gathered in one class:
export class LoginPage {
readonly email: Locator;
readonly password: Locator;
readonly submit: Locator;
constructor(private readonly page: Page) {
this.email = page.getByLabel('Email');
this.password = page.getByLabel('Password');
// the only place that knows the button name
this.submit = page.getByRole('button', { name: 'Log in' });
}
async goto() {
await this.page.goto('/login');
}
async login(email: string, password: string) {
await this.email.fill(email);
await this.password.fill(password);
await this.submit.click();
}
}
The test loses the mechanics and keeps the scenario:
test('login: success', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('qa@test.com', 'secret');
await expect(page).toHaveURL('/dashboard');
});
Now renaming the button is an edit in the line with submit, and none of the 40 tests changes. This answers the temptation to fix everything with find and replace across files: it works only on an exact string match, does not cure the cause and has to be repeated at the next change. A Page Object moves the knowledge to one place, so the edit is made once.
Where to edit when the button changed
Locators scattered across tests
Renaming the button: 40 edits and 40 chances to miss one. Tests read as a list of clicks.
The locator in one class
An edit in one line. The test names the action: loginPage.login(user), the mechanics stay inside.
Where the limits of Page Object are. POM is not "a class per URL". The boundary is set by meaning: a logical screen, a complex form, a modal. A header repeated on every page is better extracted into a separate component than duplicated in all classes (see the lesson on Page Component). A Page Object does not cure unstable tests: a bad locator in a class stays bad, it is just easier to find. It adds a layer of indirection: to understand a test, the reader opens one more file. For five to ten tests on one page that layer may not pay off, and a plain login(page, user) function is enough. So Page Object is not a dogma but a way to keep markup changes in one place once there are many tests. Alternatives that often complement it: Playwright fixtures (they create the needed class), Screenplay (tasks and actors instead of pages) and simple helper functions.
Common mistakes.
- A class per URL regardless of meaning: dozens of empty classes while the real logic is smeared around. Consequence: the same problems plus extra bureaucracy.
- A test bypasses the Page Object and uses
page.locatordirectly: a locator change again touches several places and the benefit is gone. - Building the same locator in several methods instead of one field: the copies drift apart.
- Methods named by mechanics (
clickSubmitButton) rather than intent: the name breaks together with the markup and the test does not read as a scenario.
Check yourself
A test creates new LoginPage(page) before calling page.goto(). Is there an error if the constructor builds locators with page.getByLabel('Email')?
Answer
No error. getByLabel only describes a search and looks for nothing in the DOM. The element is resolved during fill() or expect(), and only then does Playwright wait for it. That is why declaring locators in the constructor is safe.
The button was renamed, the submit line in LoginPage was fixed, yet five tests still fail. What is the likeliest cause and what to do?
Answer
Those five tests do not use the Page Object and have their own getByRole('button', { name: 'Sign in' }). The class protects only those who use it. Find them by searching for the button name and move them to LoginPage, and in code review do not let direct login locators through.