Playwright + JS · Lesson 29 of 35

0/35 done

Module 29: API Testing with Playwright

29. Direct HTTP API Automation & Hybrid API-to-UI Testing

18 minlesson

Try it yourself

Implement isSuccessStatusCode:

  1. Accept status number.
  2. Return status >= 200 && status < 300.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Pure API Testing in Playwright

javascript
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");
});

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

Does the request fixture share cookies with browser pages?

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.

JavaScript
Skip