MCPcopy Index your code
hub / github.com/NoamGaash/playwright-advanced-har

github.com/NoamGaash/playwright-advanced-har @v1.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.4.1 ↗ · + Follow
17 symbols 56 edges 13 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

🎭 playwright-advanced-har

npm version Build and test the library

Playwright-advanced-har is a library that allows various use-cases for network record-and-playback, including:

  • ignoring post data and get arguments
  • ignoring the base-url when serving HAR files (good when you record network in test environment and play on staging/production)
  • ignoring aborted requests and requests that had errors

install

npm install playwright-advanced-har

usage

The playwright-advanced-har provides a playwright fixture called advancedRouteFromHAR. Here is an example of using a custom matcher to ignore 500 status code errors:

import { test, defaultMatcher, customMatcher } from "playwright-advanced-har";

test("don't route responses that have errors", async ({ page, advancedRouteFromHAR }) => {
    await advancedRouteFromHAR("tests/har/first-has-error.har", {
        matcher: (request, entry) => {
            if (entry.response.status >= 500 && entry.response.status < 600) {
                return -1;
            }
            return defaultMatcher(request, entry);
        },
    });

    await page.goto("https://noam-gaash.co.il");
    await page.getByText("Hello World").waitFor();
});

The matcher arguments is a callback function that receives the request and the HAR entry and returns a number. The number indicates how "good" the response is. Negative numbers (such as -1) tells the router to ignore the entry, while positive numbers indicates the entry is a qualified candidate to be routed from.

examples

ignoring port numbers:

test("ignore port number", async ({ page, advancedRouteFromHAR }) => {
    await advancedRouteFromHAR("tests/har/different-port.har", {
        matcher: customMatcher({
            urlComparator(a, b) {
                a = a.replace(/:\d+/, "");
                b = b.replace(/:\d+/, "");
                return a === b;
            },
        }),
    });
    await page.goto("https://noam-gaash.co.il");
    await page.getByText("from different port").waitFor();
});

choosing arbitrary responses:

test("pick arbirtrary response", async ({ page, advancedRouteFromHAR }) => {
    // good when you're testing a long polling requests
    await advancedRouteFromHAR("tests/har/differentNumbers.har", {
        matcher: customMatcher({
            scoring() {
                return Math.random();
            },
        }),
    });
    await page.goto("https://noam-gaash.co.il");
    await page.getByText("1234").waitFor();
});

apply post-proccessing on the chosen HAR entry:

test("get the largest number... squared!", async ({ page, advancedRouteFromHAR }) => {
    // the file contains 3 responses - 42, 1234, 5
    await advancedRouteFromHAR("tests/har/differentNumbers.har", {
        matcher: {
            postProcess(entry) {
                entry.response.content.text = (parseInt(entry.response.content.text || "0") ** 2).toString();
                return entry;
            },
            matchFunction: customMatcher({
                scoring: (request, entry) => parseInt(entry.response.content.text || "0"),
            }),
        }
    });
    await page.goto("https://noam-gaash.co.il");
    await page.getByText((1234**2).toString()).waitFor();
});

for more examples, please see our tests directory

Contribution

Contributions are welcome! Please open an issue with a use-case before opening a PR, so we can discuss it. Make sure you add tests and run npm run test before commiting.

Install dependencies

npm install

Run tests

npm test

Core symbols most depended-on inside this repo

advancedRouteFromHAR
called by 32
src/index.ts
customMatcher
called by 9
src/utils/matchers/customMatcher.ts
defaultMatcher
called by 7
src/utils/matchers/defaultMatcher.ts
parseContent
called by 3
src/utils/serveFromHar.ts
scoreByHeaders
called by 2
src/utils/matchers/comparators.ts
serveFromHar
called by 1
src/utils/serveFromHar.ts
findEntry
called by 1
src/utils/serveFromHar.ts
requestResponseToEntry
called by 1
src/utils/types.ts

Shape

Function 17

Languages

TypeScript100%

Modules by API surface

src/utils/serveFromHar.ts3 symbols
tests/test.spec.ts2 symbols
tests/customMatchers.spec.ts2 symbols
tests/advancedMatchers.spec.ts2 symbols
src/utils/matchers/customMatcher.ts2 symbols
src/utils/matchers/comparators.ts2 symbols
tests/setup.spec.ts1 symbols
src/utils/types.ts1 symbols
src/utils/matchers/defaultMatcher.ts1 symbols
src/index.ts1 symbols

For agents

$ claude mcp add playwright-advanced-har \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page