Combining the API and the browser

Arrange through the API, act in the UI

Cart requests need an X-Shop-Cart header with an id you choose. Signed-in requests add Authorization: Bearer <token>.

const headers = { 'X-Shop-Cart': crypto.randomUUID() };
await request.post('/api/shop/cart/items', { headers, data: { slug: 'wireless-mouse' } });

Unique data for every test

const email = `buyer-${crypto.randomUUID()}@example.com`;

Wait for a state, not for a time

An order ships about 15 seconds after it is placed. Ask until it has changed, instead of sleeping for a guess.

await expect.poll(async () => {
  const response = await request.get(`/api/shop/orders/${id}`, { headers });
  return (await response.json()).status;
}, { timeout: 30_000 }).toBe('Shipped');
Each visitor's carts, accounts and orders are private, and reset after an hour without activity.

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

Shop API

MethodPathNotes
GET/api/shop/productsSearch with ?q, ?category, ?sort, ?page
GET/api/shop/products/:slugOne product, with options and reviews
GET/api/shop/cartThe cart. Add ?delivery=express to price delivery
POST/api/shop/cart/items{ slug, quantity, option }
PATCH / DELETE/api/shop/cart/items/:idChange the quantity, or remove the line
POST / DELETE/api/shop/cart/promo{ code }, or remove the code
POST/api/shop/register{ name, email, password } returns a token
POST/api/shop/login{ email, password } returns a token
GET/api/shop/meThe signed-in account
POST/api/shop/orders{ email, address, delivery, card, threeDS? }
GET/api/shop/ordersYour orders (needs a token)
GET/api/shop/orders/:idOne order
POST/api/shop/orders/:id/cancelOnly while Processing
GET/api/shop/inbox?email=: the confirmation emails