Structuring a test suite

A Page Object describes what a customer can do

Locators live in one place. When the page changes, one file changes, not fifty tests. Name methods after the action, not the click.

export class CartPage {
  readonly total: Locator;

  constructor(private readonly page: Page) {
    this.total = page.getByTestId('cart-total');
  }

  async applyPromo(code: string) {
    await this.page.getByLabel('Promo code').fill(code);
    await this.page.getByRole('button', { name: 'Apply' }).click();
  }
}
Keep assertions in the tests. A Page Object says how to do something; the test says what should be true.

Fixtures hand a test what it needs

export const test = base.extend<{ cart: CartPage }>({
  cart: async ({ page }, use) => {
    await use(new CartPage(page));
  },
});

test('applies a code', async ({ cart }) => { … });

Skip the slow parts through the API

Only one test needs to sign in through the form. The others can get a token from the API and place it where the app looks for it, before the page loads:

const response = await request.post('/api/shop/login', { data: { email, password } });
const { token } = await response.json();

await page.addInitScript((value) => {
  localStorage.setItem('qa-shop-token', value);
}, token);

Parallel tests must not share anything

Every worker runs at the same time. Give each test its own cart and, when it changes an account, its own account.

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