Page Component: what it is and why
A Page Component describes one block of the interface (a table, a header, a modal) and receives a root Locator from which it builds all inner locators. So it does not know its page, works on any page and can have several instances on one page without strict mode conflicts. The root is taken by role or test id, without nth, and a single-element block on one page needs no class.
Patterns · Page Component
A payments page has two identical tables. How many classes and how many copies of the locators are needed?
The payments page shows active and archived payments, and both lists are tables with the same rows, sorting and pagination. The first version: methods activeRow and archivedRow in PaymentsPage. A month later a table appears on the customers page, and the code is copied a second time. A Page Component removes those copies: a class describes one block of the interface, and the page creates as many instances as there are blocks on the screen. This lesson explains how it works, why the root is passed as a locator and where the limits of the pattern are.
How it works and why. The Page Object from the earlier lessons receives page and searches the whole page. A Page Component receives a Locator that points to the root of a block and builds every inner locator from it. Playwright supports this out of the box: Locator has the same getByRole, getByText and locator methods, and they search only inside the element found. Two properties follow. First: the component does not know where it sits, so it can be placed on any page. Second: a page can hold two instances with different roots, and they do not disturb each other.
One class, two instances
Where the knowledge about a table lives
A method per table
activeRow and archivedRow differ by one line. A new table means a new copy, and a change in the row markup is fixed twice.
One class, root from outside
Knowledge about rows, headers and sorting lives in DataTable. The page only says where the root is.
Example: from two copies to one class. The starting point: the page itself knows how each table is built.
export class PaymentsPage {
constructor(private readonly page: Page) {}
activeRow(text: string) {
return this.page
.getByTestId('active-payments')
.getByRole('row')
.filter({ hasText: text });
}
// the same, only another root: a copy per table
archivedRow(text: string) {
return this.page
.getByTestId('archived-payments')
.getByRole('row')
.filter({ hasText: text });
}
}
The logic is the same, only the root differs. If the way a row is found changes (say, by a cell rather than by row text), the edit is needed in several places, and in every page that has its own table. Now the same as a component. Four important places are marked.
import { type Locator } from '@playwright/test';
export class DataTable {
readonly rows: Locator;
1 constructor(private readonly root: Locator) {
// everything is searched only inside the root
2 this.rows = root.getByRole('row');
}
3 rowByText(text: string) {
return this.rows.filter({ hasText: text });
}
async sortBy(column: string) {
4 await this.root.getByRole('columnheader', { name: column }).click();
}
}
- The component receives a
Locator, notpage: it does not know where on the page it stands. - The rows locator is built from the root, so foreign tables do not get into the selection.
rowByTextreturns a locator, not a value: the test writesexpectwith auto-waiting itself.- The action is also inside the root: a click on another table’s header is impossible.
On the page, or right in the test, instances are created with different roots:
const active = new DataTable(page.getByTestId('active-payments'));
const archived = new DataTable(page.getByTestId('archived-payments'));
await expect(active.rowByText('INV-42')).toBeVisible();
// the same invoice must not be in the archive
await expect(archived.rowByText('INV-42')).toHaveCount(0);
The second assertion matters here: toHaveCount(0) confirms absence in this table, not on the whole page. Without a root the query would match both tables. If you try to find a row "just on the page", Playwright says so directly:
// without a root: rows of both tables match
await page.getByRole('row').filter({ hasText: 'INV-42' }).click();
// strict mode violation: getByRole('row')
// .filter({ hasText: 'INV-42' }) resolved to 2 elements
This is the same strict mode violation you meet when searching by label: a locator that found several elements cannot perform an action. A component root is a way to narrow the search without complex CSS selectors and without nth().
What to choose as a root. Best is a stable semantic anchor: getByRole('table', { name: 'Active payments' }), getByRole('dialog'), getByRole('banner') for a header, and if nothing fits, getByTestId. The root must point to one element. When a block appears later (a modal), it is not a problem: a locator is lazy, so the component can be created before the window opens and the search happens on the first action. A component may also be a table row: new OrderRow(table.rows.nth(2)) or a row found through filter, and then its methods work only within that row.
Limits and alternatives. A component is justified where a block repeats (on a page or between pages) or has its own behaviour: sorting, pagination, a date picker. A single-element block on one page needs no class: a locator field in the Page Object is shorter and reads the same. A component is not responsible for navigation and does not know the page it stands on. If it needs data from elsewhere (say, a user name to check the header), the test passes it. An alternative for many simple blocks: a helper function that takes a locator, though without state and a name such a function quickly grows into a set of scattered utilities.
Common mistakes.
- A component takes
pageand searches globally: it works only while the page has one such block, and a second table breaks every check with strict mode. - The root is too wide (
page.locator('body')) or points to several elements: the scoping benefit disappears and the error shows up in a random place. - The root is chosen with
.first()or.nth(1): once a new table appears above, all tests quietly start working with another one. - A component stores search results (ElementHandle) instead of locators: after a re-render the reference goes stale.
- Assertions with baked-in values inside a component: the same problems as in a Page Object (see the lesson on assertions).
Check yourself
A page has two tables and the component is created as new DataTable(page) with rows = page.getByRole('row'). What happens on rows.filter({ hasText: 'INV-42' }).click() if the invoice is in both tables?
Answer
The locator matches two rows, so click() throws a strict mode violation. The cause is that the search covers the whole page. The fix: the component receives the table's root Locator and builds rows from it, and then each instance sees only its own row.
A confirmation dialog appears only after a click. Can new ConfirmDialog(page.getByRole('dialog')) be created in the page constructor, before it opens?
Answer
Yes. A Locator is lazy: it only describes how to find an element and searches for nothing on creation. The search and waiting happen on the first action or assertion, so before the dialog opens the component is safe, and after that it finds the dialog by itself.