Misc — Testing APIs

Learn how to test APIs, too!

Even though Playwright is known to be an end-to-end testing tool, you can use it to test APIs, too.

The request fixture is a handy way to start making and validating requests.

test("should create a bug report", async ({ request }) => {
  const newIssue = await request.post(`/repos/${USER}/${REPO}/issues`, {
    data: {
      title: "[Bug] report 1",
      body: "Bug description",
    },
  });
  expect(newIssue.ok()).toBeTruthy();
});

Exercise

Test the products API

  1. Bring in a new api project for API tests (api.spec.js).
  2. Call the products API at /api/products/.
  3. Check for a proper 200 status code.
  4. Test that every returned product and its variants have a price.
Todo

Solution

💡 If you're stuck, find a working example on GitHub.