fix(reuse): reset tracing (#19876)

References #19059.
This commit is contained in:
Dmitry Gozman 2023-01-04 13:19:05 -08:00 committed by GitHub
parent afc1774a2b
commit 6193e6d8ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -173,6 +173,7 @@ export abstract class BrowserContext extends SdkObject {
async resetForReuse(metadata: CallMetadata, params: channels.BrowserNewContextForReuseParams | null) {
this.setDefaultNavigationTimeout(undefined);
this.setDefaultTimeout(undefined);
this.tracing.resetForReuse();
if (params) {
for (const key of paramsThatAllowContextReuse)

View File

@ -74,6 +74,11 @@ export class Snapshotter {
this._started = false;
}
resetForReuse() {
// Next time we start recording, we will call addInitScript again.
this._initialized = false;
}
async _initialize() {
for (const page of this._context.pages())
this._onPage(page);

View File

@ -110,6 +110,10 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
}
}
resetForReuse() {
this._snapshotter?.resetForReuse();
}
async start(options: TracerOptions) {
if (this._isStopping)
throw new Error('Cannot start tracing while stopping');

View File

@ -15,6 +15,7 @@
*/
import { test, expect } from './playwright-test-fixtures';
import { parseTrace } from '../config/utils';
import fs from 'fs';
test('should reuse context', async ({ runInlineTest }) => {
@ -430,3 +431,44 @@ test('should cancel pending operations upon reuse', async ({ runInlineTest }) =>
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(2);
});
test('should reset tracing', async ({ runInlineTest }, testInfo) => {
const traceFile1 = testInfo.outputPath('trace1.zip');
const traceFile2 = testInfo.outputPath('trace2.zip');
const result = await runInlineTest({
'reuse.spec.ts': `
const { test } = pwt;
test('one', async ({ page }) => {
await page.context().tracing.start({ snapshots: true });
await page.setContent('<button>Click</button>');
await page.click('button');
await page.context().tracing.stopChunk({ path: ${JSON.stringify(traceFile1)} });
});
test('two', async ({ page }) => {
await page.context().tracing.start({ snapshots: true });
await page.setContent('<input>');
await page.fill('input', 'value');
await page.locator('input').click();
await page.context().tracing.stopChunk({ path: ${JSON.stringify(traceFile2)} });
});
`,
}, { workers: 1 }, { PW_TEST_REUSE_CONTEXT: '1' });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(2);
const trace1 = await parseTrace(traceFile1);
expect(trace1.actions).toEqual([
'page.setContent',
'page.click',
]);
expect(trace1.events.some(e => e.type === 'frame-snapshot')).toBe(true);
const trace2 = await parseTrace(traceFile2);
expect(trace2.actions).toEqual([
'page.setContent',
'page.fill',
'locator.click',
]);
expect(trace2.events.some(e => e.type === 'frame-snapshot')).toBe(true);
});