Playwright setup: record and replay each test
playwrightProxy.before(page, testInfo, mode, options?)
Section titled “playwrightProxy.before(page, testInfo, mode, options?)”Call this at the start of each test (or in a beforeEach / page fixture). It sets the proxy mode for the session and, if url is provided, sets up HAR recording for browser-side requests.
await playwrightProxy.before(page, testInfo, 'replay', { // url: pattern for browser-side requests to record/replay via HAR. // // Use the ACTUAL external API domain — not the proxy URL. // Examples: // /api\.example\.com/ — your own API // /x\.com/ — record all x.com browser traffic (Chrome extension tests) // /cognito-.*amazonaws\.com/ — 3rd-party auth url: /api\.example\.com/,});url pattern: matches the real external domain that the browser calls. In record mode requests go to the real API and are saved to a .har file. In replay mode they are served from that file — no network needed. This pattern does not point to the proxy (localhost:8100).
Exception — full-stack apps: when the browser also calls localhost:8100 (because the frontend is configured with the proxy URL as its API base), use /localhost:8100/ as the pattern. Those browser requests then pass through to the proxy, which records them in the test’s .mock.json and replays them from it, so a missing one gets the proxy’s 404 rather than a HAR abort (see how replay matches requests).
Recording file names come from the test file, describe titles and title (users.spec.ts › create a user → users__create-a-user.mock.json / .har). A name over 120 bytes is cut and ends in a hash.
A test with no recording
Section titled “A test with no recording”In replay, before() throws when the test has no recording, before it switches the proxy:
[test-proxy-recorder] This test has no recording: <recordingsDir>/users__create-a-user.mock.json does not exist. Record this test first.Record the test rather than skipping it: a skipped test reports as skipped, not failed, so a missing recording goes unnoticed. hasRecording(testInfo) answers the same question without throwing, using the same file names as replay.
Global teardown (recommended)
Section titled “Global teardown (recommended)”import { playwrightProxy } from 'test-proxy-recorder';
export default async function globalTeardown() { await playwrightProxy.teardown();}export default defineConfig({ globalTeardown: './e2e/global-teardown.ts',});teardown() resets the proxy to transparent and runs the HAR redaction pass. The switch to transparent saves and ends every recording session. Don’t call it in a per-test afterAll hook under fullyParallel — see the FAQ for why that breaks parallel replay and recording.
Recording files
Section titled “Recording files”e2e/recordings/ my-test.mock.json # server-side (proxy) — SSR fetches my-test.har # client-side (HAR) — browser fetches