What is the Page Object Model?
Definition
The Page Object Model is a test automation design pattern in which each page, or significant part of a page, gets an object that holds its locators and exposes user actions as methods. Tests call those methods instead of touching selectors, so a UI change is fixed in one place rather than in every test.
How does the Page Object Model work?
Tests that call locators directly are brittle to changes in the markup, and the same fix has to be repeated in every test. Martin Fowler's Page Object entry (September 10, 2013, an older source) puts the idea in one sentence: "A page object wraps an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML."
In practice the pattern splits a suite into two layers and adds two rules:
- Page objects hold the locators and the actions a user can take:
track(orderNumber),addToCart(sku),openInvoice(id). - Tests call those actions and check the results. They contain no selectors.
- Navigation returns the next page object. Fowler writes that page object operations "should return fundamental types (strings, dates) or other page objects."
- One place per piece of markup. The Selenium docs state the goal: "there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page."
Despite the name, a page object does not have to cover a whole page. Fowler says these objects "shouldn't usually be built for each page, but rather for the significant elements on a page". His example is an album list made of album page objects, plus a header and a footer object.
Should page objects contain assertions?
Fowler notes differences of opinion here, and the sources below do not agree:
| Source | Position |
|---|---|
| Martin Fowler (2013) | "I favor having no assertions in page objects." Assertion libraries for common checks can remove the duplication instead. |
| Selenium docs | "Page objects themselves should never make verifications or assertions", with one exception: checking that the page loaded, done when the object is created. |
| Playwright docs | Their example getStarted() method clicks a link and then runs expect(...).toBeVisible() inside the page object. |
| CodeceptJS docs | Their examples include check methods in page objects, such as seeWelcomeMessage(name), which calls I.see. |
A workable middle ground: page objects return data or other page objects, and the test decides what must be true. A check like "the dashboard finished loading" can live in the page object, because it guards every test that uses it. A business rule like "the total includes tax" is easier to review when it sits in the test.
Example: a renamed field, fixed in one line
We wrote two CodeceptJS scenarios against a local order-tracking page: one for a known order number and one for an unknown one. Both use the same page object. The CodeceptJS docs say page objects should be classes. You register them under include in the config, and the dependency injection container passes them into scenarios by name.
The page object, pages/Tracking.js:
import { inject } from 'codeceptjs'
const { I } = inject()
class TrackingPage {
// Locators live here and nowhere else.
orderField = '#order'
trackButton = '#track'
status = '#status'
open() {
I.amOnPage('/track.html')
}
track(orderNumber) {
I.fillField(this.orderField, orderNumber)
I.click(this.trackButton)
}
}
export default TrackingPage
The scenarios, tracking_test.js, with include: { trackingPage: './pages/Tracking.js' } in codecept.conf.js:
Feature('Order tracking')
Scenario('a known order shows its status', ({ I, trackingPage }) => {
trackingPage.open()
trackingPage.track('A-1001')
I.see('Order A-1001: shipped', trackingPage.status)
})
Scenario('an unknown order shows an error', ({ I, trackingPage }) => {
trackingPage.open()
trackingPage.track('Z-9999')
I.see('Order not found', trackingPage.status)
})
Both passed. Then we changed the input's id in the page from order to order-number, the kind of edit a front-end refactor makes without telling anyone. Run on 2026-11-19 with CodeceptJS 4.2.5 and Playwright 1.65.1 (Chromium 159) on Node.js 22.23.5; excerpt, with stack traces and artifacts cut:
$ npx codeceptjs run
Order tracking --
✖ a known order shows its status in 160ms
✖ an unknown order shows an error in 152ms
1) Order tracking
a known order shows its status:
Field "#order" was not found by text|CSS|XPath
similar on page: #order-number
◯ Scenario Steps:
✖ I.fillField("#order", "A-1001") at TrackingPage.track (./pages/Tracking.js:15:7)
✔ I.amOnPage("/track.html") at TrackingPage.open (./pages/Tracking.js:11:7)
FAIL | 0 passed, 2 failed // 595ms
Both failures point to the same line, TrackingPage.track (./pages/Tracking.js:15:7), and CodeceptJS suggests the new id. We changed one line in the page object, orderField = '#order-number', and left both scenarios untouched:
$ npx codeceptjs run
Order tracking --
✔ a known order shows its status in 459ms
✔ an unknown order shows an error in 433ms
OK | 2 passed // 1s
With twenty scenarios behind that page object, the fix would still be one line. We also ran a Playwright Test version of the known-order check against the changed page. Its page object finds the field with getByLabel('Order number') and the button with getByRole('button', { name: 'Track' }), so the id change did not affect it, and the test passed. Locators and page objects solve different problems: here the label-based locator survived the change, and when a locator does break, the page object limits the fix to one place.
What else goes beside page objects?
CodeceptJS splits the pattern into several kinds of object, and the names are useful in any framework:
| Object | Holds | Example |
|---|---|---|
| Page object | Locators and actions for one page or area | trackingPage.track() |
| Page fragment (Selenium: page component object) | A reusable part, ideally with its own root locator | A modal box, a widget, the site header |
| Step object | A flow that spans several pages | Create a user, then change permissions |
| Custom steps on the actor | Short actions every test needs | I.login(email, password) |
CodeceptJS page objects can also define lifecycle hooks. _before() runs "only when the page object is first used in a test, not when it's injected", which suits steps like opening the page and waiting until it loads.
When does the Page Object Model hurt more than it helps?
- One class per URL, by habit. Big pages turn into huge classes. Model the parts users see, as Fowler advises.
- Pass-through methods. A
clickSubmit()that only wrapsclick('#submit')adds a layer and hides nothing. Fowler asks for "action oriented method names" instead. - Business rules hidden in page objects. When a page object asserts what the test is about, a failing test points at the wrong layer.
- Tiny or API-only suites. Three UI tests do not need an abstraction layer, and API tests have no page. The end-to-end testing entry covers which journeys deserve a UI test at all.
Page objects also make AI-drafted tests easier to review. A generated scenario that calls trackingPage.track() shows its intent, while one full of raw selectors hides it.
Page objects and reusable test steps are part of our QA and test automation work. AI agents draft scenarios, page objects and test data, and senior engineers check that each test asserts the right behavior before it joins the suite. See the CodeceptJS page for how we use the framework we created.
Sources
- Page Object, Martin Fowler (martinfowler.com), 2013-09-10
- Page object models, Selenium documentation, 2026-09-16
- Page Objects, CodeceptJS docs, 2026-04-28
- Page object models, Playwright docs (Microsoft), 2026-03-08
Frequently asked questions
Is the Page Object Model a framework?
No. It is a design pattern you apply inside a framework such as Selenium WebDriver, Playwright, Cypress or CodeceptJS. The framework drives the browser; page objects decide how your tests talk to it.
Should a page object return the next page object?
Fowler calls it common advice for navigation, and Selenium's docs follow it. He also notes that some practitioners prefer page objects to return a generic browser context and let the test decide which page object to build, especially for conditional flows.
Can the Page Object Model be used for mobile apps?
Yes. Fowler writes that the pattern applies equally well to any UI technology. The same structure works for mobile screens: locators and actions per screen, checks in the tests.
Is the Page Object Model still worth using with AI test generators?
Yes, arguably more so. Generated tests that call named page object methods are shorter and easier to review, and a locator change made by a person or an agent touches one file.
Services
Read next
Mikhail BodnarchukFounder of Codeception and CodeceptJSBoth are open-source testing frameworks: Codeception for PHP, CodeceptJS for JavaScript.LinkedInGitHub