playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: Trace network showing duplicate requests when using beforeAll/afterAll

Open jeremy-daley-kr opened this issue 1 year ago • 1 comments
trafficstars

Version

1.47.2

Steps to reproduce

Run this simple test with the --trace=on flag, so you can see the trace network requests:

import { expect, Page, test } from '@playwright/test';

test.describe('POC', async () => {
  let page: Page;

  test.beforeAll(async ({ browser }) => {
    page = await browser.newPage();
    // Intercept to see a specific request. Should only log once.
    await page.route(/redirection\.js/, async (route) => {
      console.log(route.request().url());
      await route.continue();
    });
    await page.goto('https://playwright.dev');
  });

  test.beforeEach(async () => {
    await page.goto('https://google.com/');
  });

  // Removing this makes only 2x duplication, instead of 3x
  test.afterAll(async () => {
    await page.close();
  });

  test('should work', async () => {
    await expect(page).toHaveTitle(/[a-z]/);
  });
});

Expected behavior

Trace network requests should match the intercepted requests logged... only 1.

Actual behavior

All trace network requests appear to be tripled, even though the intercepted console logs in the test show just one. I don't believe the requests are actually firing 3 times... it just appears that way.

Image

Additional context

No response

Environment

System: OS: macOS 14.4.1 Memory: 80.97 MB / 16.00 GB Binaries: Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node Yarn: 3.6.1 - ~/.nvm/versions/node/v18.17.1/bin/yarn npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm Languages: Bash: 3.2.57 - /bin/bash npmPackages: playwright: 1.47.2 => 1.47.2 playwright-lighthouse: ^4.0.0 => 4.0.0

jeremy-daley-kr avatar Oct 15 '24 03:10 jeremy-daley-kr

I can repro. More minimal repro:

diff --git a/tests/playwright-test/playwright.trace.spec.ts b/tests/playwright-test/playwright.trace.spec.ts
index 5c5d6c304..973bdc252 100644
--- a/tests/playwright-test/playwright.trace.spec.ts
+++ b/tests/playwright-test/playwright.trace.spec.ts
@@ -287,6 +287,36 @@ test('should work in serial mode', async ({ runInlineTest }, testInfo) => {
   expect(fs.existsSync(testInfo.outputPath('test-results', 'a-serial-fails', 'trace.zip'))).toBeTruthy();
 });
 
+test('should not produce duplicate network requests in serial mode', async ({ runInlineTest, server }) => {
+  const result = await runInlineTest({
+    'playwright.config.ts': `
+        module.exports = { use: { trace: 'retain-on-failure' } };
+      `,
+    'a.spec.ts': `
+        import { Page, test } from '@playwright/test';
+
+        let page: Page;
+        test.beforeAll(async ({ browser }) => {
+          page = await browser.newPage();
+        });
+
+        test('should work', async () => {
+          await page.goto('${server.EMPTY_PAGE}');
+        });
+        
+        // This resulted in the duplication
+        test.afterAll(() => {});
+        test.afterAll(() => {});
+        test.afterAll(() => {});
+        test.afterAll(() => {});
+      `,
+  }, { workers: 1, trace: 'on' });
+  expect(result.exitCode).toBe(0);
+  const trace = await parseTrace(test.info().outputPath('test-results', 'a-should-work', 'trace.zip'));
+  debugger
+  expect(trace.model.resources.map(resource => resource.request.url)).toEqual([server.EMPTY_PAGE]);
+});
+
 test('should not override trace file in afterAll', async ({ runInlineTest, server }, testInfo) => {
   const result = await runInlineTest({
     'playwright.config.ts': `

Good: 1.45.0

Bad: 1.46.0

diff: https://github.com/microsoft/playwright/compare/1b4d9003c63ad91b38b42b255759345a8d28d91c...37ffbd757e732b52c3497ff65009fbe3a5743df5

mxschmitt avatar Oct 15 '24 08:10 mxschmitt

Investigation notes: this is because we do not reset network trace to keep snapshots working, and then merge network traces from beforeAll and test that have the same entries.

dgozman avatar Dec 12 '24 17:12 dgozman

hello 👋 Is there a known workaround for this or should we just be patient wait for a fix? This increasing the size of our trace files since our app makes many requests. In our example, we man handle our own browser/page and set retries to 1 (retain traces on failure)

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  retries: 1,
  reporter: 'html',
  use: {
    trace: 'retain-on-failure',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
});

import { test, expect, Browser, Page, chromium } from "@playwright/test";
let browser: Browser;
let page: Page;
test.beforeAll(async ({}) => {
  browser = await chromium.launch();
  const context = await browser.newContext();
  page = await context.newPage();
  await page.goto("https://www.perplexity.ai/");
});

test("dupe network requests in trace files", async () => {
  await expect(page.getByText("should fail?")).toBeVisible();
});

Image

ryanrosello-og avatar Jan 18 '25 01:01 ryanrosello-og

@yury-s and @mxschmitt Following the activity, I'm a little confused on the status of this. Was this fixed with https://github.com/microsoft/playwright/pull/34616 ? If so, do you know what version it would be included in?

jeremy-daley-kr avatar Mar 18 '25 14:03 jeremy-daley-kr

Was this fixed with #34616 ?

No, that PR fixed a different issue.

yury-s avatar Mar 18 '25 15:03 yury-s

Why was this issue closed?

Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.

Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.

pavelfeldman avatar Sep 04 '25 01:09 pavelfeldman