Skip to content

Commit 002fc12

Browse files
authored
fix: allow trace in showBrowser mode (#542)
1 parent f851f36 commit 002fc12

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

src/testModel.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -531,25 +531,16 @@ export class TestModel extends DisposableBase {
531531
const externalOptions = await this._embedder.runHooks.onWillRunTests(this.config, false);
532532
const showBrowser = this._embedder.settingsModel.showBrowser.get() && !!externalOptions.connectWsEndpoint;
533533

534-
let trace: 'on' | 'off' | undefined;
535-
let video: 'on' | 'off' | undefined;
536-
537-
if (this._embedder.settingsModel.showTrace.get())
538-
trace = 'on';
539-
// "Show browser" mode forces context reuse that survives over multiple test runs.
540-
// Playwright Test sets up `tracesDir` inside the `test-results` folder, so it will be removed between runs.
541-
// When context is reused, its ongoing tracing will fail with ENOENT because trace files
542-
// were suddenly removed. So we disable tracing in this case.
543-
if (this._embedder.settingsModel.showBrowser.get()) {
544-
trace = 'off';
545-
video = 'off';
546-
}
547-
548534
const options: PlaywrightTestRunOptions = {
549535
headed: showBrowser && !this._embedder.isUnderTest,
550536
workers: showBrowser ? 1 : undefined,
551-
trace,
552-
video,
537+
// Note: we used to disable trace in "show browser" mode, but it's not necessary anymore:
538+
// - output directory is not removed;
539+
// - tracesDir is not respected when connecting to a reused browser.
540+
trace: this._embedder.settingsModel.showTrace.get() ? 'on' : undefined,
541+
// Disable video when reusing context, because video cannot be chunked between multiple
542+
// tests that use a single context.
543+
video: this._embedder.settingsModel.showBrowser.get() ? 'off' : undefined,
553544
reuseContext: showBrowser,
554545
connectWsEndpoint: showBrowser ? externalOptions.connectWsEndpoint : undefined,
555546
};

tests/run-tests.spec.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,22 +1206,30 @@ test('should produce output twice', async ({ activate, overridePlaywrightVersion
12061206
`);
12071207
});
12081208

1209-
test('should disable tracing when reusing context', async ({ activate, showBrowser }) => {
1210-
test.skip(!showBrowser);
1211-
1209+
test('should not crash with tracing no matter reusing context', async ({ activate }) => {
12121210
const { testController } = await activate({
12131211
'playwright.config.js': `module.exports = { testDir: 'tests', use: { trace: 'on' } }`,
1214-
'tests/test.spec.ts': `
1212+
'tests/test1.spec.ts': `
12151213
import { test } from '@playwright/test';
12161214
test('one', async ({ page }) => {});
12171215
`,
1216+
'tests/test2.spec.ts': `
1217+
import { test } from '@playwright/test';
1218+
test('two', async ({ page }) => {});
1219+
`,
12181220
});
12191221

1220-
const testItems = testController.findTestItems(/test.spec.ts/);
1221-
expect(testItems.length).toBe(1);
1222-
await testController.run(testItems);
1222+
const testItems1 = testController.findTestItems(/test1.spec.ts/);
1223+
expect(testItems1.length).toBe(1);
1224+
await testController.run(testItems1);
1225+
expect(fs.existsSync(test.info().outputPath('test-results', 'test1-one', 'trace.zip'))).toBe(true);
12231226

1224-
expect(fs.existsSync(test.info().outputPath('test-results', 'test-one', 'trace.zip'))).toBe(false);
1227+
const testItems2 = testController.findTestItems(/test2.spec.ts/);
1228+
expect(testItems2.length).toBe(1);
1229+
await testController.run(testItems2);
1230+
// We do not clear output dir, so both traces should be present.
1231+
expect(fs.existsSync(test.info().outputPath('test-results', 'test1-one', 'trace.zip'))).toBe(true);
1232+
expect(fs.existsSync(test.info().outputPath('test-results', 'test2-two', 'trace.zip'))).toBe(true);
12251233
});
12261234

12271235
test('should force workers=1 when reusing the browser', async ({ activate, showBrowser }) => {

0 commit comments

Comments
 (0)