A test receives page in its first parameter, and page has a type that you never wrote. The Junior track explained why: test declares what its callback receives.
A fixture is a value that Playwright prepares for a test and hands to it through that parameter. page is a fixture that comes with Playwright. You can add your own, such as a page object that is ready to use. When you do, you also have to tell the compiler what it is.
This lecture has no practice exercise. A fixture needs Playwright's test, and the practice editor on this site does not have it. Try each step in your own project.
The problem
Every test in a file starts with the same two lines.
import { test } from "@playwright/test";
import { LoansPage } from "./loans-page";
test("a member can return a book", async ({ page }) => {
const loans = new LoansPage(page);
await loans.open();
await loans.returnBook("Dune");
});
You want the test to ask for loans and receive it opened.
The type comes first
Start with a type that lists your fixtures: each name, and the type of the value a test receives.
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");
}
}
export type ShelfFixtures = {
loans: LoansPage;
};
That much is checked on this site against Playwright's own types. It is an ordinary type, built from the page object of lecture 5.
test.extend
The rest follows the Playwright documentation. You make your own test from Playwright's, and pass the fixture type between angle brackets.
import { test as base } from "@playwright/test";
import { LoansPage } from "./loans-page";
type ShelfFixtures = {
loans: LoansPage;
};
export const test = base.extend<ShelfFixtures>({
loans: async ({ page }, use) => {
const loans = new LoansPage(page);
await loans.open();
await use(loans);
},
});
export { expect } from "@playwright/test";
Read the middle part slowly.
base.extend<ShelfFixtures>is a call with a type argument. Lecture 3 showed that form.loans:is the fixture. Its name must be a name inShelfFixtures.async ({ page }, use) => { ... }is the function that prepares it. It receives the existing fixtures and a function calleduse.await use(loans)hands the value to the test. Code after that line runs when the test has finished, which is where clean-up goes.
Here the type argument does real work. use accepts only a LoansPage, because that is what ShelfFixtures says. Hand it something else and the compiler reports it. Apply the test from lecture 3. This type parameter links two things: the type you declared and the value you pass.
The test gets the type
Tests import test from your file.
import { test, expect } from "./fixtures";
test("a member can return a book", async ({ loans }) => {
await loans.returnBook("Dune");
await expect(loans.heading).toBeVisible();
});
No annotation, and loans is a LoansPage. Your editor completes its methods, and a misspelled method is an error. This is the same inference that types page.
Two things follow from how Playwright is built, and both are in its documentation. A fixture is set up only for a test that asks for it. And a fixture that should be shared by all tests in a worker is declared in a second type argument, with a scope option on the fixture.
Where the compiler trusts you
The compiler checks the shape: the name exists, and the value passed to use has the right type. It does not check what the fixture did.
If the function forgets await loans.open(), the types are identical. The test receives a LoansPage that is on no page. If it never calls use, the test does not run at all, and the type has nothing to say about it.
So a fixture type tells you what a test holds. It says nothing about the state that value is in. That part is checked by running the tests.
Remember this
List your fixtures in one type and pass it to test.extend between angle brackets. Every test that imports your test then receives each fixture with its type. The compiler checks the name and the type of the value, and nothing about what the fixture set up.
Reference: Fixtures in the Playwright documentation.