Playwright + TS · Lesson 29 of 36
Module 29: API Testing with Playwright
Try it yourself
Implement isSuccessfulHttpStatus:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
request FixturePlaywright exposes request: APIRequestContext directly into tests:
import { test, expect } from "@playwright/test";
test("create user and verify response", async ({ request }) => {
const response = await request.post("https://api.targetqa.com/users", {
data: {
name: "Alex QA",
email: "alex@targetqa.com",
role: "SDET",
},
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
},
});
expect(response.status()).toBe(201);
expect(response.ok()).toBeTruthy();
const body = await response.json();
expect(body.name).toBe("Alex QA");
expect(body.id).toBeDefined();
});Instead of clicking through 15 forms to create a project before testing a comment box:
request.post() in 100ms./projects/${id} in the browser!test("hybrid workflow", async ({ request, page }) => {
// 1. Create order via API
const res = await request.post("/api/orders", { data: { item: "Book", qty: 1 } });
const { orderId } = await res.json();
// 2. Verify in UI
await page.goto(`/orders/${orderId}`);
await expect(page.getByRole("heading", { name: `Order #${orderId}` })).toBeVisible();
});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 before launching the UI. Let's master hybrid API and UI testing!
Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Yes! If you use 'page.request', it shares the exact same cookie store as the page. If you use the standalone 'request' fixture, it has its own isolated HTTP context.
Absolutely. Playwright's API testing features provide TypeScript typings, automatic cookie handling, and seamless integration with browser tests in the same test runner.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.