fix: reset APIRequestContext network trace between chunks

Fixes https://github.com/microsoft/playwright/issues/34404
This commit is contained in:
Yury Semikhatsky 2025-02-04 10:51:12 -08:00
parent dc14490f13
commit e6ee1d0419
2 changed files with 43 additions and 2 deletions

View file

@ -272,9 +272,14 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
state.chunkOrdinal = 0; // Reset ordinal for the new name.
this._allocateNewTraceFile(state);
// Network file survives across chunks, so make a copy with the new name.
// - Browser context network trace is shared across chunks as it contains resources
// used to serve page snapshots, so make a copy with the new name.
// - APIRequestContext network traces are chunk-specific, always start from scratch.
const newNetworkFile = path.join(state.tracesDir, name + '.network');
this._fs.copyFile(state.networkFile, newNetworkFile);
if (this._context instanceof BrowserContext)
this._fs.copyFile(state.networkFile, newNetworkFile);
else
this._fs.writeFile(state.networkFile, '');
state.networkFile = newNetworkFile;
}

View file

@ -160,3 +160,39 @@ test('should display list of query parameters (only if present)', async ({ runUI
await expect(page.getByText('Query String Parameters')).not.toBeVisible();
});
test('should not duplicate network entries from beforeAll', {
annotation: [
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34404' },
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33106' },
]
}, async ({ runUITest, server }) => {
const { page } = await runUITest({
'playwright.config.ts': `
module.exports = { use: { trace: 'on' } };
`,
'a.spec.ts': `
import { test as base, expect, request, type APIRequestContext } from '@playwright/test';
const test = base.extend<{}, { apiRequest: APIRequestContext }>({
apiRequest: [async ({ }, use) => {
const apiContext = await request.newContext();
await use(apiContext);
await apiContext.dispose();
}, { scope: 'worker' }]
});
test.beforeAll(async ({ apiRequest }) => {
await apiRequest.get("${server.EMPTY_PAGE}");
});
test('first test', async ({ }) => { });
test.afterAll(async ({ apiRequest }) => { });
`,
});
await page.getByText('first test').dblclick();
await page.getByText('Network', { exact: true }).click();
await expect(page.getByTestId('network-list').getByText('empty.html')).toHaveCount(1);
});