Skip to content
SDETRole
SDETRole

Test the cart API with the request fixture

intermediate API testing Challenge 19 of 22

The cart API has a single smoke test that only checks for a 2xx response. Write API tests with Playwright’s request fixture that would catch the three planted bugs.

The cart API

Request Expected response
POST /api/cart with { "productId": 4 } 201, JSON cart: { items: [{ id, name, price, qty }], count, total, ... }
Same product again 201, the existing line’s qty increases (still one line)
POST /api/cart with an unknown productId 404, { "error": "Unknown product" }

Product 4 is the Wireless Mouse at 35. The server starts each test with an empty cart. Use relative URLs: the base URL is the demo app.

Requirements

  • Use the request fixture (no page needed).
  • Assert exact status codes: 201 for an add, 404 for an unknown product.
  • Assert the response bodies, including the quantity after a repeated add.

Expected output

Tests check the 201 status and body of a successful add, the quantity after a repeated add, and the 404 for an unknown product. Each planted API bug fails at least one test.

Scenarios (4)

Your tests run once against each version of the demo app. They must pass on the working app and fail on every version with a planted bug.

  • ○Working API · must pass
  • ○Bug: POST returns 200, not 201 · must catch
  • ○Bug: unknown product crashes (500) · must catch
  • ○Bug: quantity never increases · must catch

Checks (2)

  • ○Asserts the exact 201 status, not just ok()
  • ○Asserts the 404 for an unknown product
example.spec.tsRuns against a live demo app

Ctrl/⌘ + Enter to run tests · your code is saved in this browser

Results

Run the tests to see results here.

Demo app

The working app. You can click around it; your tests start from a fresh copy.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →