PW Fixture Designer
This repo already has a fixtures module. Extend it, do not create a second one.
Read first
src/fixtures/test-base.ts. It exports the project's test and expect, and holds two kinds of
fixture, which is the distinction to preserve:
- Page-object fixtures construct a POM against
pageand navigate nothing. - State fixtures perform reusable setup and are chained:
validLogin->loginWithInventory->loginWithSelectedItem.invalidLoginstands alone.
Fixtures are lazy: Playwright builds only what a test asks for, plus its dependencies. That is the whole reason the chain is worth keeping, so never collapse it into one big setup fixture.
Workflow
- Decide which kind you are adding. A POM wrapper is a page-object fixture; anything that logs in, seeds, or navigates is a state fixture.
- Add the field to the exported
TestFixturetype. Give state fixtures a named exported type when they return more than a POM, asSelectedItemStatedoes. - Add the
base.extendentry. Depend on existing fixtures by destructuring them rather than repeating their steps. - Put anything after
await use(...)that must be torn down. Nothing here needs teardown today; if yours does, that is where it goes. - Credentials come from
@config/credentialsor@testdata/logintestdata.json. Never hard-code them in a fixture.
Output shape
export type CheckoutReadyState = {
cartPage: CartPage;
itemId: string;
};
export type TestFixture = {
// ...existing fields
checkoutReady: CheckoutReadyState;
};
export const test = base.extend<TestFixture>({
// Depends on loginWithSelectedItem, so login + inventory + add-to-cart all run first.
checkoutReady: async ({ loginWithSelectedItem, cartPage }, use) => {
await cartPage.open();
await use({ cartPage, itemId: loginWithSelectedItem.itemId });
},
});
Verify
npx tsc --noEmit -p tsconfig.json
npx playwright test src/tests/e2e/e2e-checkout_new_fixture.spec.ts
The second command matters: it is the spec that exercises the existing fixture chain, so it is the one that catches a regression in it.