Workflow:Microsoft Playwright API testing
| Knowledge Sources | |
|---|---|
| Domains | API_Testing, REST_API, Test_Automation |
| Last Updated | 2026-02-11 22:00 GMT |
Overview
End-to-end process for testing REST APIs and HTTP endpoints using Playwright's built-in request context, without requiring browser interaction.
Description
This workflow covers using Playwright's APIRequestContext (available as the request fixture) to test HTTP APIs directly. Unlike browser-based testing, API testing sends HTTP requests programmatically and validates responses without rendering pages. The workflow supports authentication via custom headers, request lifecycle management through beforeAll/afterAll hooks for resource setup and cleanup, and comprehensive response validation including status codes, JSON body parsing, and header inspection.
Usage
Execute this workflow when you need to verify backend API behavior independently from the UI layer, create test data via APIs before running E2E tests, or validate API contracts. This is particularly useful for testing RESTful services, verifying CRUD operations, and ensuring API responses match expected schemas.
Execution Steps
Step 1: Configure API request context
Set up the API testing context by configuring the base URL and authentication headers using test.use(). The base URL eliminates the need to repeat full URLs in each request call. Authentication is typically configured via custom HTTP headers (e.g., Bearer tokens, API keys).
Key considerations:
- Use baseURL to set the API root endpoint
- Configure extraHTTPHeaders for authentication tokens
- Headers are automatically included in all requests made through the context
- API keys and tokens should be loaded from environment variables, not hardcoded
Step 2: Set up test resources
Use test.beforeAll() to create any resources needed for the test suite via API calls. This may include creating test users, repositories, datasets, or other entities that tests will interact with. Store references (IDs, URLs) for use in subsequent tests.
Key considerations:
- beforeAll runs once per worker, not per test
- Use request.post() to create resources
- Validate creation responses with expect(response.ok()).toBeTruthy()
- Store resource identifiers for cleanup in afterAll
Step 3: Execute API requests
Write individual test cases that make HTTP requests (GET, POST, PUT, PATCH, DELETE) to the API endpoints. Each test verifies a specific API behavior or endpoint. Pass request bodies as JSON objects via the data parameter.
Key considerations:
- Use request.get(), request.post(), request.put(), request.delete()
- Pass JSON payloads via the data option
- Each test should focus on a single API operation or assertion
- Request context handles cookies and redirects automatically
Step 4: Validate API responses
Assert response status codes, parse JSON bodies, and verify the structure and content of API responses. Use Playwright's expect assertions for response validation, including checking that response collections contain expected objects.
Key considerations:
- Use response.ok() for status code validation (2xx)
- Use response.json() to parse response bodies
- Use expect(array).toContainEqual(expect.objectContaining({...})) for partial matching
- Validate response headers when needed with response.headers()
Step 5: Clean up test resources
Use test.afterAll() to delete or clean up any resources created during the test suite. This ensures a clean state for subsequent test runs and prevents resource accumulation.
Key considerations:
- afterAll runs once per worker after all tests complete
- Use request.delete() to remove created resources
- Validate cleanup responses to ensure successful deletion
- Consider idempotent cleanup that does not fail if resources are already gone