QA Interview Hub · Knowledge base · Quizzes · Українська

Patterns › Page Component and composition

Composition instead of inheritance

Composition is more reliable than inheritance: a page declares the components it has, while a BasePage imposes its contents on every descendant. TypeScript will not notice that an inherited header is absent from the login page, so the error shows only at run time. The base class stays thin (page and goto), shared setup goes to fixtures, and chains deeper than two levels hide behind a page method.

Patterns · Page Component

The login page inherited BasePage with a header, but there is no header on it. What breaks first?

A team makes a BasePage with a header, a footer and a side menu because "they are everywhere". A week later a login page appears without any of them, and then a modal with its own table. Now the login class has three locators that are not in the DOM, and the modal code duplicates a table because the base does not have it. This lesson shows why "has-a" is more reliable than "is-a" and how to assemble a page from components.

How it works and why. Inheritance fixes the composition of a page when the class is declared: whatever the ancestor has, the descendant has too, and it cannot be removed. In TypeScript a class has only one ancestor, so a page that needs a header from one base class and a table from another cannot be assembled. Composition works the opposite way: the class declares the component fields it needs and creates them from root locators. The page decides its own composition, and components know nothing about their owners. A change in one does not spread through a hierarchy.

How a page gets a header

Wrong

Inherit from BasePage

The header exists in all descendants, even where there is none. Changing the base touches every page.

Right

Have a Header field

The page takes only what is on the screen. Components do not depend on pages and can be combined.

Figure 1. It is better to declare the page composition than to inherit it.

The composition of three pages

LoginPageno componentsPaymentsPageheader + active + archivedSettingsPageheader + confirmDialog
Figure 2. Each page assembles its own set. An empty LoginPage carries nothing extra, and SettingsPage nests a dialog.

Example: from BasePage to components. The starting point. A seemingly convenient base where one missing detail breaks the logic.

export class BasePage {
  readonly header: Header;
  readonly footer: Footer;
  readonly menu: SideMenu;

  constructor(protected readonly page: Page) {
    this.header = new Header(page.getByRole('banner'));
    this.footer = new Footer(page.getByRole('contentinfo'));
    this.menu = new SideMenu(page.getByRole('navigation'));
  }
}

// the login page has neither a header nor a menu
export class LoginPage extends BasePage {}

The problem is not the class itself but the assumption that all pages are alike. A test on the login page may accidentally call login.header.openProfileMenu() and fail on a timeout, while the editor hint says the method exists. Now composition. Four important places are marked.

export class PaymentsPage {
1  readonly header: Header;
  readonly active: DataTable;
  readonly archived: DataTable;

  constructor(private readonly page: Page) {
2    this.header = new Header(page.getByRole('banner'));
    this.active = new DataTable(page.getByTestId('active-payments'));
    this.archived = new DataTable(page.getByTestId('archived-payments'));
  }

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

export class SettingsPage {
  readonly header: Header;
  // a component on the page: the dialog owns its buttons
3  readonly confirm: ConfirmDialog;

  constructor(page: Page) {
    this.header = new Header(page.getByRole('banner'));
4    this.confirm = new ConfirmDialog(page.getByRole('dialog'));
  }
}
  1. The field declares that the header is on this page. The login page does not have this field.
  2. The header root is set in one place of the page and does not depend on others.
  3. The dialog is a component too: it owns its buttons and may contain nested components of its own.
  4. The modal is created only where it occurs.

The test matches what the user sees:

test('archive a payment from the list', async ({ page }) => {
  const payments = new PaymentsPage(page);
  await payments.goto();
  await payments.header.openProfileMenu();
  await expect(payments.active.rowByText('INV-42')).toBeVisible();
});

The chain payments.header.openProfileMenu() reads as a description of the screen: page, its part, action. This form stays short while nesting is no deeper than two levels. Three or four dots in a row (page.a.b.c.d()) show that the test knows too much about the page structure; then add a method on the page that hides the path.

Limits and alternatives. A thin base is justified if it does only what is shared by all pages: it accepts page, has goto() and maybe path. Then it holds no components and has few reasons to change.

// a thin base: only what everyone really needs
export abstract class BasePage {
  constructor(protected readonly page: Page) {}

  abstract readonly path: string;

  async goto() {
    await this.page.goto(this.path);
  }
}

The criterion is simple: does every descendant need every property of the base. If not, its place is in a component. For shared setup (login, data) Playwright fixtures fit better: they create the needed objects and clean them up after the test. Composition has a cost too: more files, and for a page with one locator field a component class is excessive. Do not create a component "for growth" if the block exists only on one page and is unlikely to repeat: extracting it later is easy, removing an unneeded abstraction is harder.

Common mistakes.

  • A "universal" BasePage with a header, a footer and a menu: the login page inherits what it does not have, and one changed locator in the base touches everyone.
  • A hierarchy of three or four levels: to find a method the reader walks through several files, and a change at a lower level breaks the upper one.
  • A component receives a reference to the owning page and calls its methods: the dependency becomes circular and the component cannot be used on its own.
  • Long a.b.c.d chains in tests: the test knows too much about the internal structure. Add a method on the page.

Check yourself

The login page inherits BasePage, which has this.header. A developer writes login.header.openProfileMenu() and gets a timeout. Why did TypeScript not warn and what should change?

Answer

For TypeScript the header field exists because it is in the ancestor class. The compiler cannot see that the element is missing from the login page DOM, so the error appears only at run time. The fix: remove header from the base and declare it only on pages where it really exists.

What minimum responsibility is worth keeping in a page base class if you cannot avoid one?

Answer

Only what every page needs without exception: receiving page through the constructor and navigation (goto by path). Everything about individual interface blocks moves into components and is attached by composition.

In an interview. Junior: I prefer composition: a page has component fields, such as a header or a table, and does not inherit them. Then each page takes only what it has. Middle/Senior: Inheritance fixes the composition at class declaration, and TypeScript has one ancestor, so a login page without a header already breaks BasePage. I assemble components from root locators and do not let them know their owners. I keep the base class thin: page and goto. Shared setup goes to fixtures, and chains deeper than two levels I hide behind a page method.

More articles in this chapter