Practice Playwright API Testing
3 lectures
Curriculum . Playwright . API Testing

Playwright API Testing

API testing inside the same Playwright project that already runs your UI tests. One CI pipeline, one reporter, one trace viewer - but you can hit a REST service directly without a browser. Build a token, post a booking, validate a schema, replay a HAR file, all from @playwright/test.

3
Deep lectures
CRUD, Auth + Schema, Network monitoring + HAR.
15
Mermaid diagrams
Sequence + flow per lecture; 2 on this hub.
26
Drill exercises
Real targets: restful-booker, Spotify, TTACart.
1
Cloneable framework
Advance-Playwright-Framework on api-testing-pw.

01What is API testing with Playwright

API testing means hitting a service's HTTP endpoints directly - no browser, no DOM. You POST JSON, GET resources, assert status codes, validate response shapes, then move on. Playwright happens to ship an excellent HTTP client called the request fixture. It is built on the same network stack as the browser context, so cookies, proxies, and certificates work uniformly across your UI and API specs.

Why use Playwright instead of Postman or REST Assured?

Three reasons most teams give up Postman collections and Java REST Assured suites for Playwright once they have one Playwright project running:

  • Same runner, same reporter. API and UI tests show up in the same HTML report. CI does not need a second runner.
  • Same language. If your team writes TypeScript for the UI, they write TypeScript for the API. No context switch to a JSON GUI or Java DSL.
  • Trace viewer applies. Every API call shows up in the trace timeline with request body, response body, and headers. Postman has no equivalent.
CapabilityPostmanREST Assured (Java)Playwright request
Lives next to UI testsnonoyes
TypeScript nativepartialnoyes
Trace viewer per requestnonoyes
Auth state reused from a UI sign-innonoyes (storage state)
Built-in JSON schema validatorpartial (Postman scripts)pluginvia AJV
CI costlicense per seatfreefree
flowchart LR
  TEST[Playwright test file] --> REQ[request fixture]
  REQ --> CTX[APIRequestContext]
  CTX --> NET[HTTP stack]
  NET --> API[Target REST API]
  API -.JSON.-> NET
  NET -.parsed.-> CTX
  CTX -.Response.-> TEST
  TEST --> EXP[expect assertions]
The request fixture is a thin HTTP client wrapped by APIRequestContext
sequenceDiagram
  participant T as test('book a stay')
  participant R as request
  participant API as restful-booker
  T->>R: POST /auth { username, password }
  R->>API: HTTP POST
  API-->>R: 200 { token: 'abc123' }
  R-->>T: response
  T->>R: POST /booking + Authorization: Bearer abc123
  R->>API: HTTP POST
  API-->>R: 200 { bookingid, booking }
  R-->>T: response
  T->>R: GET /booking/{id}
  R->>API: HTTP GET
  API-->>R: 200 { booking }
  R-->>T: response
  Note over T: assertion: response.json().firstname === 'Pramod'
One round trip per assertion - auth, create, verify

02The three lectures

Each lecture builds on the last. Lecture 1 teaches the request fixture itself. Lecture 2 adds authentication strategies and schema validation. Lecture 3 inverts the direction - intercepting the browser's network traffic instead of making the calls ourselves.

03Clone the framework

Every code snippet in these lectures lives in a real, runnable Playwright framework. The dedicated branch api-testing-pw ships with the booking client, the OAuth2 token helper, AJV schemas, HAR fixtures, and a small CI workflow.

Clone

git clone --branch api-testing-pw https://github.com/PramodDutta/Advance-Playwright-Framework.git
cd Advance-Playwright-Framework
npm install
npx playwright test --project=api

Open the api-testing-pw branch on GitHub

04The API testing pyramid

Most teams over-invest in UI tests and under-invest in API tests. The classic Mike Cohn pyramid still holds: unit at the base, integration / API in the middle, full UI at the top. API tests run 10-100x faster than UI tests and are far less flaky.

UI tests (E2E) slow, flaky, expensive API + integration Playwright request fixture fast, stable, cheap Unit tests millisecond runs vitest / jest / mocha slower faster most tests live here
The API band is where Playwright's request fixture earns its place
test() body async ({ request }) request APIRequestContext REST API restful-booker POST HTTP 200 JSON response expect(response).toBeOK() expect(await response.json()).toMatchSchema(BookingSchema)
Each test gets a fresh request context unless you set storageState

05Minimal setup

Three files. That's the whole footprint.

package.json

package.json
{
  "name": "pw-api",
  "scripts": { "test": "playwright test" },
  "devDependencies": {
    "@playwright/test": "^1.49.0",
    "ajv": "^8.17.1",
    "ajv-formats": "^3.0.1"
  }
}

playwright.config.ts

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  timeout: 30_000,
  use: {
    baseURL: 'https://restful-booker.herokuapp.com',
    extraHTTPHeaders: { 'Accept': 'application/json' },
  },
  projects: [
    { name: 'api', testMatch: /.*\.api\.spec\.ts/ },
  ],
});

tests/booking.api.spec.ts

tests/booking.api.spec.ts
import { test, expect } from '@playwright/test';

test('ping the heartbeat endpoint', async ({ request }) => {
  const res = await request.get('/ping');
  expect(res.status()).toBe(201);
});

That's the entire shape. Lecture 1 builds the full booking flow on top of this.