HAR to Playwright: mocks and tests from a HAR

Capture the failing flow once, then turn the HAR into Playwright code: page.route stubs for the API calls, a routeFromHAR bundle that replays them, or a regression test skeleton for each finding. HAR Playground generates all three in your browser from the redacted capture.

Last updated:

Open HAR PlaygroundFree, no signup. Runs in your browser.

Steps

  1. Capture the flowRecord the problem with the steps in how to capture a HAR file.
  2. Open it in HAR PlaygroundDrop the file on HAR Playground. Secrets are redacted before anything is exported.
  3. Open Export and pick a Playwright formatChoose Playwright routes for page.route stubs, Playwright routeFromHAR for an API-only HAR plus a snippet, or Regression tests for one test per finding.
  4. Copy or download the codePaste the snippet into your spec, or save the .api.har next to your tests.
  5. Run the testRun npx playwright test and replace any [REDACTED:...] placeholders with test values.

page.route: mock one API call

Use page.route when you want to force one response, such as the 500 that broke checkout, and check how the page copes.

Stub a failing API call
import { test, expect } from '@playwright/test';

test('checkout shows an error when the orders API returns 500', async ({ page }) => {
  await page.route('**/api/orders', (route) =>
    route.fulfill({
      status: 500,
      contentType: 'application/json',
      body: JSON.stringify({ error: 'internal' }),
    }),
  );
  await page.goto('https://shop.example/checkout');
  await expect(page.getByRole('alert')).toBeVisible();
});

routeFromHAR: replay a whole capture

Use page.routeFromHAR to serve many responses from a saved HAR, so a test does not need a live backend. Set notFound: 'fallback' so requests that are not in the file go to the network as normal.

Replay API responses from a HAR
import { test, expect } from '@playwright/test';

test('dashboard renders from a recorded API', async ({ page }) => {
  await page.routeFromHAR('https://shop.example/fixtures/dashboard-api.har', {
    url: '**/api/**',
    notFound: 'fallback',
  });
  await page.goto('https://shop.example/dashboard');
  await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});

Tips for stable tests

  • Keep only the API calls the test needs. A smaller HAR is easier to review and breaks less often.
  • Replace redacted placeholders with fixed test data, and never commit real tokens.
  • Re-record the HAR when the API changes. Playwright can also update it for you with update: true.
  • Prefer a regression test with a clear assertion over a bare replay, so the test fails for the right reason.

Catch broken requests before your users do

TestChimp watches your QA posture on every pull request and feeds failures back to your coding agent until they are fixed.

Start your free trial

14-day free trial. No credit card required.