Manuals / Playwright with Python / Chapter 26
Part 4 · Advanced Techniques · advanced · ~50 min · Chapter 26 of 61
18. API Testing with Playwright
request context (APIRequestContext) Playwright ships a full HTTP client independent of any browser — meaning you can make API calls without opening a page at all, useful both for pure API testing and for fast test-data setup (create data via API instead of clicking through slow UI forms). import pytest from playwright.sync_api import sync_playwright with sync_playwright() as p: request_context = p
Step 1 of 6
Overview
request context (APIRequestContext) Playwright ships a full HTTP client independent of any browser — meaning you can make API calls without opening a page at all, useful both for pure API testing and for fast test-data setup (create data via API instead of clicking through slow UI forms). Or via the pytest-playwright plugin's built-in request fixture: extra_http_headers=...) What it does: Creates an APIRequestContext — an isolated HTTP client session for making API calls. Types/params: calls, e.g. set once instead of repeating the full domain every call this context, commonly used for auth tokens: {"Authorization": "Bearer <token>"} directly into Chapter 20) Pointers: Like BrowserContext, an APIRequestContext is isolated — separate contexts don't share cookies/headers, letting you cleanly simulate different authenticated users in the same test file.
- base_url (string, optional) — prefix applied to relative URLs in subsequent
- extra_http_headers (dict, optional) — headers sent with every request from
- storage_state (string or dict, optional) — reuse saved cookies/auth state (ties
Try it
Run / study this snippet
import pytest
from playwright.sync_api import sync_playwrightChapter learning outcomes
- GET/POST/PUT/DELETE calls — full detail
- Content-Type: application/json header for you
- POST new_user = response.json()
- Shared response object reference (response)
- Combining UI + API tests
Clear these before you leave
Side quest
18. API Testing with Playwright deliverable
Apply one idea from “Overview” in a small script or note.