Playwright + JS · Lesson 29 of 35
Module 29: API Testing with Playwright
Try it yourself
Implement isSuccessStatusCode:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
import { test, expect } from "@playwright/test";
test("validate user creation API", async ({ request }) => {
const response = await request.post("https://api.targetqa.com/users", {
data: { name: "Alice", role: "QA Engineer" },
headers: { Authorization: "Bearer token-123" }
});
expect(response.status()).toBe(201);
const body = await response.json();
expect(body.name).toBe("Alice");
});Playwright is not just for browsers; it also includes a blazing-fast HTTP API client. In this lesson, we will use the request fixture to validate REST APIs directly, and combine API calls with browser tests to seed test data in milliseconds. Let's master hybrid API and UI testing!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
If you use page.request, it shares the exact same cookies as the browser page. If you use the standalone request fixture, it has its own isolated cookie context.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.