fix: reset APIRequestContext network trace between chunks (#34616)

This commit is contained in:
Yury Semikhatsky 2025-02-05 00:00:04 -08:00 committed by GitHub
parent 11913e6f39
commit fb3e8ed114
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 4 deletions

View File

@ -170,10 +170,16 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
this._state.recording = true;
this._state.callIds.clear();
// - 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 preserveNetworkResources = this._context instanceof BrowserContext;
if (options.name && options.name !== this._state.traceName)
this._changeTraceName(this._state, options.name);
this._changeTraceName(this._state, options.name, preserveNetworkResources);
else
this._allocateNewTraceFile(this._state);
if (!preserveNetworkResources)
this._fs.writeFile(this._state.networkFile, '');
this._fs.mkdir(path.dirname(this._state.traceFile));
const event: trace.TraceEvent = {
@ -267,14 +273,14 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
state.traceFile = path.join(state.tracesDir, `${state.traceName}${suffix}.trace`);
}
private _changeTraceName(state: RecordingState, name: string) {
private _changeTraceName(state: RecordingState, name: string, preserveNetworkResources: boolean) {
state.traceName = name;
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.
const newNetworkFile = path.join(state.tracesDir, name + '.network');
this._fs.copyFile(state.networkFile, newNetworkFile);
if (preserveNetworkResources)
this._fs.copyFile(state.networkFile, newNetworkFile);
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);
});