Rayrun
← Back to Discord Forum

What data from the request is used to find request from HAR?

I want to run tests with network data routed from a HAR file. They used to succeed, but now they're failing and I can't figure out why. I added the following code to my test case:

page.on('request', (request) =>
    console.log('>>', request.method(), request.url())
  );
  page.on('response', (response) =>
    console.log('<<', response.status(), response.url())
  );

and I get console about the request, but not response. If I change the fixture from

await page.routeFromHAR(harFilePath, {
        notFound: 'abort',
        url: apiUrlsGlob,
      });

to

await page.routeFromHAR(harFilePath, {
        notFound: 'fallback',
        url: apiUrlsGlob,
      });

then I also get response, but it's a failing response, because my test cases uses anonymized data etc.

The HAR archive I'm using is loaded in before test is run and contains an entry that should route correctly, but my suspicion is that it's not being used because it's not matched. What attributes need to be identical between a request being made in the code and request/response data in the HAR file for it to be used?

P.S.: the link to "replaying from har" is broken here: https://playwright.dev/docs/api/class-browsercontext#browser-context-route-from-har

This thread is trying to answer question "What attributes need to be identical between a request being made in the code and request/response data in the HAR file for it to be used?"

0 replies

Answer

Matching Requests with HAR Files in @playwright/test

To match a request in your code with data in a HAR file, you need to ensure a few things.

URL Matching

First, the URL of the request must match exactly or follow a specified glob pattern. If the URL in your code differs from the one in the HAR file, the response won't be served.

// Example of URL matching in @playwright/test
const { test } = require('@playwright/test');

test('URL should match', async ({ page }) => {
  await page.goto('https://ray.run/');
  // rest of the code
});

HTTP Method Matching

Second, the HTTP method must match exactly. If the HAR file records a GET request but your code makes a POST request, they won't match.

// Example of HTTP method matching in @playwright/test
await page.goto('https://ray.run/');
const [response] = await Promise.all([
  page.waitForResponse('https://ray.run/api/data'),
  page.click('text=Submit'),  // this triggers a POST request
]);

POST Requests

For POST requests, the URL, HTTP method, and payload must all match. If any of these differ, the requests won't be considered identical.

// Example of POST request matching in @playwright/test
await page.goto('https://ray.run/');
await page.fill('input[name="data"]', 'payload');
await page.click('text=Submit');  // this triggers a POST request with payload

Multiple Recordings

If multiple recordings match a request, the one with the most matching headers is picked.

Remember, each response should have its own entry for different scenarios or variations of requests. If these attributes differ during the replaying from HAR process, it won't work as expected.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].