Writing a long test that stays readable

Test one journey, and check it along the way

An end-to-end test follows a real customer. Assert at each stage, so that when it fails, you know which stage broke.

Name the stages with test.step()

Steps appear in the report and the trace, so a failure reads like a story.

await test.step('Find the laptop', async () => {
  await page.getByRole('searchbox', { name: 'Search products' }).fill('Nimbus 14');
  await page.getByRole('button', { name: 'Search' }).click();
  await page.getByRole('link', { name: 'Nimbus 14 Laptop' }).click();
});

Find one product among many

Every product has an "Add to cart" button. Scope to the right one:

await page
  .getByRole('list', { name: 'Products' })
  .getByRole('listitem')
  .filter({ hasText: 'Wireless Mouse' })
  .getByRole('button', { name: 'Add to cart' })
  .click();

Payment forms live in iframes

const payment = page.getByTitle('Secure payment').contentFrame();
await payment.getByLabel('Card number').fill('4111 1111 1111 1111');

Reuse what the app generates

An order number is different on every run. Read it from the page, then use it in the next assertion.

const orderNumber = await page.getByTestId('order-number').textContent();
await expect(page.getByText(`Your order ${orderNumber} is confirmed`)).toBeVisible();
Keep few tests like this. They are slow and touch everything. Cover the rules (promo codes, validation) in smaller tests.

The shop

QA Electronics runs at /shop. Nothing is really sold, and each browser context gets its own cart.

Open the shop

Accounts

EmailPasswordWhat it has
alex@example.comPassword1A saved address and past orders
locked@example.comPassword1A locked account

Promo codes

CodeEffect
SAVE1010% off
WELCOME20$20 off orders of $200 or more
FREESHIPFree standard delivery
SUMMER25Expired

Test cards

Use any future expiry date, such as 12/30, and any 3-digit CVC.

Card numberResult
4111 1111 1111 1111Success (Visa)
4242 4242 4242 4242Success (Visa)
5555 5555 5555 4444Success (Mastercard)
4000 0000 0000 0002Declined
4000 0000 0000 9995Insufficient funds
4000 0000 0000 0069Expired card
4000 0000 0000 3220Bank asks to confirm (3-D Secure)
4000 0000 0000 0119Provider times out after 6 seconds