Facade and business steps
A Facade in tests is a class of business steps that composes several Page Objects into one action and owns no locators. It removes a repeated click sequence from dozens of tests, and test.step gives the report and trace a readable step name. The facade hides preconditions, not what the test verifies: the assertion stays in the test.
Patterns · Facade
Thirty tests start with the same eight clicks of a transfer. Where should they live?
A test about transfer history first has to make a transfer: open an account, press “Transfer”, fill two fields, confirm. These eight lines repeat in thirty tests. A Page Object already hides the locators, but the sequence across two pages stays in every test. When the confirmation gets one more screen, all thirty need an edit. A Facade removes exactly this repetition: one call instead of a sequence.
How it works and why. A Facade gives a simple interface to a complex subsystem. In tests the subsystem is several Page Objects and API calls, and the facade is a “steps” class with business-language methods: sendMoney, passKyc. It owns no locators: everything about a page stays in its Page Object, and the facade only composes an action from them. So a UI change is fixed in one Page Object, a change of the scenario itself (a new screen appeared) in one facade method, and the tests are not touched.
The second contribution is test.step: it groups actions under one name in the HTML report and the trace. A failure reads as “send 100 from UA001 to UA002”, not as a click on an unknown button.
Where the Facade sits
Example: before and after. First, the test without a facade. Seven of eight lines are about the transfer, and only one is about what is checked:
test('history lists a fresh transfer', async ({ page }) => {
await page.goto('/accounts/UA001');
await page.getByRole('button', { name: 'Transfer' }).click();
await page.getByLabel('To').fill('UA002');
await page.getByLabel('Amount').fill('100');
await page.getByRole('button', { name: 'Confirm' }).click();
await page.goto('/history');
await expect(page.getByRole('row').first()).toContainText('100.00');
});
Now the steps class. Four places matter.
export class TransferSteps {
1 constructor(
private readonly accounts: AccountsPage,
private readonly transfer: TransferPage,
) {}
async sendMoney(from: string, to: string, amount: number) {
return test.step(
2 'send ' + amount + ' from ' + from + ' to ' + to,
async () => {
await this.accounts.open(from);
await this.accounts.clickTransfer();
await this.transfer.fill(to, amount);
3 return this.transfer.confirm(); // returns the transfer reference
},
4 { box: true },
);
}
}
- The facade receives Page Objects in its constructor and does not create them: it is easy to assemble in a fixture.
- The step name contains the data, so the report shows which transfer failed.
test.stepreturns what the callback returned: the transfer reference can be used in the test.{ box: true }: the error points to thesendMoneycall line in the test, not to the clicks inside the step.
The facade is plugged in as a fixture (see the lesson on fixtures), and the test shrinks to what it checks:
export const test = base.extend<{ steps: TransferSteps }>({
steps: async ({ page }, use) => {
await use(
new TransferSteps(new AccountsPage(page), new TransferPage(page)),
);
},
});
test('history lists a fresh transfer', async ({ page, steps }) => {
const ref = await steps.sendMoney('UA001', 'UA002', 100);
await new HistoryPage(page).open();
await expect(page.getByTestId('row-' + ref)).toContainText('100.00');
});
Limits and alternatives. The main rule: a facade hides preconditions and repeated end-to-end paths, not what the test checks. If a test is about validation of the transfer form, it works with TransferPage directly, because the fields and messages are the subject of the check.
What to hide in a facade
A Facade for a precondition
A test about transfer history calls sendMoney: the transfer is needed but it is not what is checked.
A Facade hides the check itself
A limit test calls sendMoneyAndCheckLimit: neither the data nor the expectation is visible in the test body.
If a precondition does not need the UI, it is better done through the API (next lesson): that is faster and more stable than clicking. A UI facade is justified when the path through the interface is part of the scenario or there is no other entry. The Cypress analogue is custom commands, in Selenium it is “step” classes; the idea is the same.
Common mistakes.
- Layers
Test → Steps → Actions → Pages → Componentswhere each one just forwards the call: indirection without abstraction, and the real click is hard to find while debugging. - A facade method containing
expectfor several things at once: when it fails it is unclear what is wrong, and a test about something else suddenly checks the transfer. - A facade per page (“AccountsSteps”, “TransferSteps” with one method): that is a Page Object under another name. A facade should combine several pages.
- One giant steps class for the whole app: it becomes a god object. Cut it by domain: transfers, KYC, cards.
- A step without
test.step: the report and the trace show raw clicks again, and the main benefit of the facade is lost.
Check yourself
A test about amount validation calls steps.sendMoney('UA001', 'UA002', -5) and expects an error. What is wrong?
Answer
The facade assumes the transfer succeeds and hides the fields and messages that are the subject here. The test should work with TransferPage directly: fill, confirm, expect on the error message.
What changes in the report if test.step is removed from sendMoney and the rest is left as is?
Answer
The logic works the same, but the report and the trace show separate clicks and fills instead of the step “send 100 from UA001 to UA002”. The group is gone, so it is harder to see at which stage of the transfer it failed.
test.step for the report. Middle/Senior: A facade in tests is for preconditions and repeated paths, not for the check itself: expect and the data being verified stay visible in the test. I make sure it does not become one more forwarding layer, and where possible I replace UI preconditions with API calls.