Skip to content

Commit fb3e8ed

Browse files
authored
fix: reset APIRequestContext network trace between chunks (#34616)
1 parent 11913e6 commit fb3e8ed

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

packages/playwright-core/src/server/trace/recorder/tracing.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
170170
this._state.recording = true;
171171
this._state.callIds.clear();
172172

173+
// - Browser context network trace is shared across chunks as it contains resources
174+
// used to serve page snapshots, so make a copy with the new name.
175+
// - APIRequestContext network traces are chunk-specific, always start from scratch.
176+
const preserveNetworkResources = this._context instanceof BrowserContext;
173177
if (options.name && options.name !== this._state.traceName)
174-
this._changeTraceName(this._state, options.name);
178+
this._changeTraceName(this._state, options.name, preserveNetworkResources);
175179
else
176180
this._allocateNewTraceFile(this._state);
181+
if (!preserveNetworkResources)
182+
this._fs.writeFile(this._state.networkFile, '');
177183

178184
this._fs.mkdir(path.dirname(this._state.traceFile));
179185
const event: trace.TraceEvent = {
@@ -267,14 +273,14 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
267273
state.traceFile = path.join(state.tracesDir, `${state.traceName}${suffix}.trace`);
268274
}
269275

270-
private _changeTraceName(state: RecordingState, name: string) {
276+
private _changeTraceName(state: RecordingState, name: string, preserveNetworkResources: boolean) {
271277
state.traceName = name;
272278
state.chunkOrdinal = 0; // Reset ordinal for the new name.
273279
this._allocateNewTraceFile(state);
274280

275-
// Network file survives across chunks, so make a copy with the new name.
276281
const newNetworkFile = path.join(state.tracesDir, name + '.network');
277-
this._fs.copyFile(state.networkFile, newNetworkFile);
282+
if (preserveNetworkResources)
283+
this._fs.copyFile(state.networkFile, newNetworkFile);
278284
state.networkFile = newNetworkFile;
279285
}
280286

tests/playwright-test/ui-mode-test-network-tab.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,39 @@ test('should display list of query parameters (only if present)', async ({ runUI
160160

161161
await expect(page.getByText('Query String Parameters')).not.toBeVisible();
162162
});
163+
164+
test('should not duplicate network entries from beforeAll', {
165+
annotation: [
166+
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34404' },
167+
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33106' },
168+
]
169+
}, async ({ runUITest, server }) => {
170+
const { page } = await runUITest({
171+
'playwright.config.ts': `
172+
module.exports = { use: { trace: 'on' } };
173+
`,
174+
'a.spec.ts': `
175+
import { test as base, expect, request, type APIRequestContext } from '@playwright/test';
176+
177+
const test = base.extend<{}, { apiRequest: APIRequestContext }>({
178+
apiRequest: [async ({ }, use) => {
179+
const apiContext = await request.newContext();
180+
await use(apiContext);
181+
await apiContext.dispose();
182+
}, { scope: 'worker' }]
183+
});
184+
185+
test.beforeAll(async ({ apiRequest }) => {
186+
await apiRequest.get("${server.EMPTY_PAGE}");
187+
});
188+
189+
test('first test', async ({ }) => { });
190+
191+
test.afterAll(async ({ apiRequest }) => { });
192+
`,
193+
});
194+
195+
await page.getByText('first test').dblclick();
196+
await page.getByText('Network', { exact: true }).click();
197+
await expect(page.getByTestId('network-list').getByText('empty.html')).toHaveCount(1);
198+
});

0 commit comments

Comments
 (0)