Test framework layers
The layers of a test framework separate things that change for different reasons: the scenario, the markup, the API contract and the environment. An upper layer knows about a lower one, not the reverse: a test describes the business scenario, a Page Object hides locators and actions, and fixtures supply data created through the API. A new layer is added when duplication appears a third time.
System design · Framework
A button label changed and 200 tests needed edits. Where should the line between a test and the details run?
Login and the transfer form are copied into two hundred tests. Design renamed the «Sign in» button and the whole suite turned red. Playwright has nothing to do with it: the tests knew too much. This lesson covers how to split a framework into layers so a change in one place does not spread over the whole suite, and where that split stops paying off.
How it works and why. A layer exists to separate things that change for different reasons. A scenario changes when a business rule changes. Locators change when the markup changes. HTTP requests change with the API contract, and addresses and secrets with the environment. If everything sits in one file, any of these changes touches everything. The dependency rule is one: an upper layer knows about a lower one, never the other way round. A Page Object does not import a test, and a test does not know what a locator looks like.
Layers and the direction of dependencies
The top layer is read by a reviewer and by a new colleague, so it should be a short story about the business. Reporters and notifications stand to the side: they subscribe to run results and are not in the dependency chain.
Example: before and after. Here is a test that knows everything:
test('transfer of 30 leaves 70', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('qa1@example.test');
await page.getByLabel('Password').fill('secret');
await page.getByRole('button', { name: 'Sign in' }).click();
await page.getByRole('link', { name: 'Transfer' }).click();
await page.getByLabel('Amount').fill('30');
await page.getByRole('button', { name: 'Send' }).click();
await expect(page.getByTestId('balance')).toHaveText('70.00');
});
Every line here belongs to a different layer: login is a precondition, the page path and labels are UI details, and the only thing the scenario really verifies is the last line. Now the same test split into layers.
test('transfer of 30 leaves 70', async ({ funded, transferPage }) => {
await transferPage.open();
await transferPage.send({ to: 'savings', amount: 30 });
await expect(transferPage.balance).toHaveText('70.00');
});
// pages/transfer.page.ts
export class TransferPage {
readonly balance: Locator;
constructor(private page: Page) {
this.balance = page.getByTestId('balance');
}
async open() { await this.page.goto('/transfer'); }
async send(t: { to: string; amount: number }) {
await this.page.getByLabel('To').fill(t.to);
await this.page.getByLabel('Amount').fill(String(t.amount));
await this.page.getByRole('button', { name: 'Send' }).click();
}
}
The test gets the precondition «a signed-in user with a balance» from a fixture. With test.extend you describe what a test receives ready, and the fixture cleans up after the test itself:
type Fixtures = { api: BankApi; funded: User; transferPage: TransferPage };
export const test = base.extend<Fixtures>({
api: async ({ request }, use) => {
await use(new BankApi(request));
},
funded: async ({ api, page }, use) => {
const user = await api.createUser();
await api.deposit(user.accountId, 100);
await api.signIn(page.context(), user); // this user's session
await use(user); // the test runs here
await api.deleteUser(user.id); // cleanup after the test
},
transferPage: async ({ page }, use) => {
await use(new TransferPage(page));
},
});
The same test before and after
The test knows everything
Locators, URLs and data right in the test. A button label changed: edits in 200 files.
The test reads as a scenario
Locators in a Page Object, data in a fixture. The label changed: one line in one class.
Why preconditions are created through the API rather than by UI steps: a user, KYC and a top-up are one or two requests instead of dozens of clicks, and those requests do not depend on the markup. If a precondition fails, the test is marked as broken in setup, not as a failure of the scenario under test. The UI stays only where it is the subject of the check.
Limits and alternatives. Layers have a price: more files, more jumps through references, more places to hide bad code. For a suite of 30 tests three parts are enough: tests, Page Objects and fixtures. A flows layer (business steps like makeTransfer) appears when the same chain of steps repeats in many tests. A practical rule: extract when duplication shows up for the third time, not «for the future». A Page Object need not match a whole page: for repeated parts (a menu, modal windows) component objects are handier. Scenario asserts stay in the tests, while a page class returns locators and actions. An alternative to classes is helper functions and fixtures without a Page Object; Playwright allows both approaches.
Common mistakes.
- A Page Object of five hundred lines with every action of the site: it cannot be read, and any edit touches everyone.
- A test that imports a locator or calls
page.locatoritself: the scenario layer again knows details and the gain from the split is gone. - Fixtures with hidden shared state (one user for all tests): the layer does not isolate, it hides the cause of conflicts.
- An API client with the checking business logic moved into it: the test stops being independent of the implementation.
- Layers for the sake of layers in a small project: maintenance costs more than the duplication.
Check yourself
A test has the line page.getByRole('button', { name: 'Sign in' }).click(). Which principle is broken and what happens when the label changes?
Answer
The top layer (the scenario) knows a locator, which is a detail of a lower layer. The label changed, so every test with this line must be fixed. If the locator lives in a Page Object, there is one edit.
Why is it better to create the precondition «a user with balance 100» in a fixture through the API than with UI steps at the start of the test?
Answer
Through the API it is a few requests with no dependency on the markup, while UI steps duplicate other scenarios and are slow and brittle. A failure in setup is not confused with a failure of the scenario under test, and the fixture reliably cleans the data after the test. The UI is used only for what the test itself verifies.