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();
});
Note
The Playwright docs come with extensive API testing examples. Make sure to check them out!
Exercise
Test the products API
- Bring in a new api project for API tests (
api.spec.js). - Call the products API at
/api/products/. - Check for a proper 200 status code.
- Test that every returned product and its variants have a price.
Todo
Solution
💡 If you're stuck, find a working example on GitHub.