Skip to main content

Quiz: API testing with Playwright

Why This Matters

A quick check that this module’s ideas hold up before you move on. Answer each question yourself before you open the answer.

Quiz

1. Why prepare test data through the API in a UI test?

  • A. API calls are more realistic than clicks
  • B. It is faster and less fragile, and the UI is only used for the behaviour under test
  • C. Playwright cannot click buttons reliably
  • D. It avoids the need for assertions
Show answer
B. Setup through the API removes slow UI steps that are not what the test is about, and that can fail for unrelated reasons.
Quiz

2. An API returns 200 with an empty body instead of the cart. Your test checks only the status. What happens?

  • A. The test fails
  • B. The test passes while the API is broken
  • C. Playwright warns about the empty body
  • D. The test is marked flaky
Show answer
B. A status check alone proves little. Check the shape with a schema and the values that matter.
Quiz

3. What does const body = (await response.json()) as Product guarantee?

  • A. That the body is a valid product
  • B. Nothing at runtime: it only tells TypeScript what you expect
  • C. That missing fields get default values
  • D. That extra fields are removed
Show answer
B. as is a promise to the type checker, not a check. ProductSchema.parse(body) checks the data at runtime and fails with the exact field that is wrong.
Quiz

4. Why does every API test get its own X-Session-Id?

  • A. The API requires a new id per request
  • B. So tests running in parallel do not share a cart, orders or stock
  • C. To make the tests faster
  • D. To log in
Show answer
B. With a sandbox per test, a test that places an order cannot change the stock another test is checking. That is what makes fullyParallel safe.
Quiz

5. An error test asserts on error.message (“Only 20 of Bluetooth Headphones left in stock.”). What is the risk?

  • A. None; the message is the most precise check
  • B. The test breaks when someone rewords the message, although the behaviour did not change
  • C. Messages are not returned in JSON
  • D. The message is only sent in English
Show answer
B. Assert on the stable code (INSUFFICIENT_STOCK) and, where useful, on details[].field. Wording changes; codes are part of the contract.
Quiz

6. Twenty tests start from the same saved storageState. Some of them change the cart. What goes wrong?

  • A. Nothing; each test gets a copy of the cart
  • B. They share one session, so the result depends on the order the tests run in
  • C. Playwright refuses to run them in parallel
  • D. The saved file is overwritten
Show answer
B. Storage state copies the cookies, and the cookie points at one server-side session. Use saved state as a read-only starting point, and give tests that change data a sandbox of their own.