You move three repeated lines from a test into a helper. The compiler stops you on the first line.
export async function openCatalog(page): Promise<void> {
await page.goto("/catalog");
}
catalog.ts(1,35): error TS7006: Parameter 'page' implicitly has an 'any' type.
Inside a test, page had a type and you never wrote one. In your helper it is a plain parameter, and TypeScript has nothing to read.
Many people write page: any here to make the error go away. Then the compiler checks nothing that the helper does with the page. A misspelled method compiles, and the test fails when it runs.
Import the type
Playwright ships its types inside the package. You import the ones you need by name. The examples in this lecture are checked against Playwright's own types.
import type { Page } from "@playwright/test";
export async function openCatalog(page: Page): Promise<void> {
await page.goto("/catalog");
await page.getByRol("heading", { name: "Catalog" }).click();
}
catalog.ts(5,14): error TS2551: Property 'getByRol' does not exist on type 'Page'. Did you mean 'getByRole'?
With page: Page, the compiler knows every method of a page. It found the misspelled one and offered the right name. Your editor now completes method names inside the helper, as it does inside a test.
Why a test needs no annotation
In a test file you write this, with no type on page.
import { test, expect } from "@playwright/test";
test("the catalog opens", async ({ page }) => {
await page.goto("/catalog");
await expect(page.getByRole("heading", { name: "Catalog" })).toBeVisible();
});
Remember the callback in lecture 3. TypeScript knew the type of its parameter from the function it was passed to. The same thing happens here. test declares what its callback receives, so page is already a Page.
A helper is your own function. Nothing declares its parameters except you.
Page and Locator
Playwright has two types you will use every day.
Pageis the whole browser tab.Locatoris a description of one element, or a group of elements, on the page.
page.getByRole(...) returns a Locator. A locator has getByRole too, and it searches inside that element only.
import type { Locator, Page } from "@playwright/test";
export function loanRow(page: Page, title: string): Locator {
return page.getByRole("row", { name: title });
}
export async function returnBook(row: Locator): Promise<void> {
await row.getByRole("button", { name: "Return" }).click();
}
loanRow is not async and has no await. Making a locator does not talk to the browser, so it returns a Locator and not a promise. click() does talk to the browser, so returnBook waits for it.
When you write a helper, ask what it really needs. A helper that works on one row should take a Locator. It can then be used on any row, and a reader can see it does not touch the rest of the page.
Read what a method returns
A framework's return types tell you things that are easy to miss. Hover over a method in your editor, or let the compiler tell you.
import type { Locator } from "@playwright/test";
export async function coverLink(row: Locator): Promise<string> {
return row.getByRole("link").getAttribute("href");
}
catalog.ts(4,3): error TS2322: Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
The last line is the one to read. getAttribute gives text, or null when the element has no such attribute. This is lecture 9 and lecture 6 together: await takes the value out of the promise, and then you still have to handle the missing case.
Decide what a missing value means for your test. Here a book with no cover link is a bug, so the helper stops with a clear message.
import type { Locator } from "@playwright/test";
export async function coverLink(row: Locator): Promise<string> {
const href = await row.getByRole("link").getAttribute("href");
if (href === null) {
throw new Error("The cover link has no href");
}
return href;
}
Other frameworks
The idea is the same in every typed framework. The package brings the types, and you import the names. Cypress and Selenium have their own types for the things they hand you. Read the TypeScript page of the framework's documentation for the names.
Remember this
A framework ships the types for what it gives you. Import Page and Locator with import type and use them on your helper's parameters, never any. Read the return type of a method before you use its result, because it tells you about promises and about null.
Reference: TypeScript in the Playwright documentation.