UI + API Together
Use each tool where it is strongest: the API to set things up and check results, the browser for what a customer really sees.
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.
Accounts
| Password | What it has | |
|---|---|---|
| alex@example.com | Password1 | A saved address and past orders |
| locked@example.com | Password1 | A locked account |
Promo codes
| Code | Effect |
|---|---|
| SAVE10 | 10% off |
| WELCOME20 | $20 off orders of $200 or more |
| FREESHIP | Free standard delivery |
| SUMMER25 | Expired |
Test cards
Use any future expiry date, such as 12/30, and any 3-digit CVC.
| Card number | Result |
|---|---|
| 4111 1111 1111 1111 | Success (Visa) |
| 4242 4242 4242 4242 | Success (Visa) |
| 5555 5555 5555 4444 | Success (Mastercard) |
| 4000 0000 0000 0002 | Declined |
| 4000 0000 0000 9995 | Insufficient funds |
| 4000 0000 0000 0069 | Expired card |
| 4000 0000 0000 3220 | Bank asks to confirm (3-D Secure) |
| 4000 0000 0000 0119 | Provider times out after 6 seconds |
Shop API
| Method | Path | Notes |
|---|---|---|
| GET | /api/shop/products | Search with ?q, ?category, ?sort, ?page |
| GET | /api/shop/products/:slug | One product, with options and reviews |
| GET | /api/shop/cart | The cart. Add ?delivery=express to price delivery |
| POST | /api/shop/cart/items | { slug, quantity, option } |
| PATCH / DELETE | /api/shop/cart/items/:id | Change 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/me | The signed-in account |
| POST | /api/shop/orders | { email, address, delivery, card, threeDS? } |
| GET | /api/shop/orders | Your orders (needs a token) |
| GET | /api/shop/orders/:id | One order |
| POST | /api/shop/orders/:id/cancel | Only while Processing |
| GET | /api/shop/inbox | ?email=: the confirmation emails |
Related exercises
- Network & APIAdvancedAPI TestingTest the practice API directly with the request fixture, without a browser.
- Mini ShopAdvancedPage Objects & FixturesTurn a long test into a maintainable suite with Page Objects and custom fixtures.
- FormsIntermediateFile DownloadCatch downloads and check their names and contents.
Read the guide: Playwright Page Object Model with fixtures