lecture 5 of 110 completed

Typing page objects

A typed page object declares its fields, marks its locators readonly and hides its helpers. The compiler then keeps tests out of its details.

What you'll learn

  • How to declare and type the fields of a class
  • What readonly and private stop a test from doing
  • What private does not protect when the code runs

A page object is a class that holds the locators and actions of one page, so tests do not repeat them. Here is one for the Shelf loans page, written the way it would be in JavaScript.

export class LoansPage {
  constructor(page) {
    this.page = page;
  }
}
loans-page.ts(2,15): error TS7006: Parameter 'page' implicitly has an 'any' type.
loans-page.ts(3,10): error TS2339: Property 'page' does not exist on type 'LoansPage'.

There are two errors, and the second is new. In JavaScript, assigning to this.page creates the field. In TypeScript a class must declare its fields first. That one rule removes a whole kind of mistake. A misspelled field name inside a method no longer creates a second field with no warning.

Declare the fields

The examples in this lecture are checked against Playwright's own types.

import type { Locator, Page } from "@playwright/test";

export class LoansPage {
  readonly page: Page;
  readonly heading: Locator;

  constructor(page: Page) {
    this.page = page;
    this.heading = page.getByRole("heading", { name: "Loans" });
  }

  async open(): Promise<void> {
    await this.page.goto("/loans");
  }

  async returnBook(title: string): Promise<void> {
    await this.row(title).getByRole("button", { name: "Return" }).click();
  }

  private row(title: string): Locator {
    return this.page.getByRole("row", { name: title });
  }
}

Read it from the top.

  • The two lines under class declare the fields and their types. These are the types from the Junior lecture on framework types.
  • The constructor takes a Page and fills in both fields.
  • open and returnBook talk to the browser, so they are async and return Promise<void>.
  • row only builds a locator. It is not async and returns a Locator.

The compiler checks that the constructor fills in every declared field.

import type { Locator, Page } from "@playwright/test";

export class LoansPage {
  readonly page: Page;
  readonly heading: Locator;

  constructor(page: Page) {
    this.page = page;
  }
}
unfinished-page.ts(5,12): error TS2564: Property 'heading' has no initializer and is not definitely assigned in the constructor.

Without this check, heading would be undefined in the first test that used it.

readonly

A test should use a locator. It should not replace one.

import type { Page } from "@playwright/test";
import { LoansPage } from "./loans-page";

export async function check(page: Page): Promise<void> {
  const loans = new LoansPage(page);
  loans.heading = page.getByRole("heading", { name: "Members" });
}
loans.test.ts(6,9): error TS2540: Cannot assign to 'heading' because it is a read-only property.

readonly allows the constructor to set the field once. After that, nothing may assign to it.

private

row is a detail. If tests call it, they depend on how the page object finds a row, and you cannot change that later without editing the tests.

import type { Page } from "@playwright/test";
import { LoansPage } from "./loans-page";

export async function check(page: Page): Promise<void> {
  const loans = new LoansPage(page);
  await loans.row("Dune").click();
}
loans.test.ts(6,15): error TS2341: Property 'row' is private and only accessible within class 'LoansPage'.

A member with no word in front of it is public. Mark the actions a test needs as public by leaving them alone, and mark everything else private. A reviewer reads the public members of a page object as its promise to the tests.

What private does not protect

private is checked by the compiler and removed with the other types.

class Counter {
  private count = 0;

  add(): void {
    this.count++;
  }
}

const counter = new Counter();
counter.add();
console.log((counter as any).count);
1

One as any and the private field is readable. At run time it is an ordinary field. So private stops mistakes in code the compiler checks. It does not stop code that goes around the compiler.

JavaScript has its own private fields, written with a # in front of the name. Those are enforced when the code runs. You will see both in real projects.

The short form you will read

Many page objects are written like this.

import type { Page } from "@playwright/test";

export class LoansPage {
  constructor(private readonly page: Page) {}

  async open(): Promise<void> {
    await this.page.goto("/loans");
  }
}

A word such as private or readonly in front of a constructor parameter declares the field and fills it in, in one place. It is shorter, and you need to be able to read it.

It is also different from everything else in this lecture. A type annotation is removed and leaves the code as it was. This form makes TypeScript write the line this.page = page for you. Lecture 8 shows why that difference matters.

Remember this

Declare each field of a class with its type, and let the compiler check that the constructor fills it in. readonly stops a test from replacing a locator, and private keeps a helper inside the class. Both are checked by the compiler only. They are gone when the code runs.

Reference: Page object models in the Playwright documentation.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.