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.
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.
Capability
Postman
REST Assured (Java)
Playwright request
Lives next to UI tests
no
no
yes
TypeScript native
partial
no
yes
Trace viewer per request
no
no
yes
Auth state reused from a UI sign-in
no
no
yes (storage state)
Built-in JSON schema validator
partial (Postman scripts)
plugin
via AJV
CI cost
license per seat
free
free
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.
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
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.
The API band is where Playwright's request fixture earns its placeEach test gets a fresh request context unless you set storageState
That's the entire shape. Lecture 1 builds the full booking flow on top of this.
DDrills - hub orientation
Read the request fixture API reference. Open the official Playwright docs page for APIRequestContext. List the eight methods it exposes (get / post / put / patch / delete / head / fetch / dispose).
Confirm reachability. From your local machine, run curl https://restful-booker.herokuapp.com/ping -v and confirm the server returns HTTP/1.1 201 Created. If you see a TLS error, your corporate network proxy is blocking it - hop onto a personal connection.
Clone the framework. Follow the clone block above, switch to branch api-testing-pw, run npm install, and run npx playwright test --project=api --list. Confirm at least 12 tests are discovered.
Pick your lecture order. Read the lecture cards above. Most students start with CRUD and finish with Network. If your project today is mocking responses, jump straight to Network.