Skip to content

Commit 0fe7a10

Browse files
committed
cherry-pick(#31437): fix(electron): tracing with @playwright/test
1 parent 4ec4ccf commit 0fe7a10

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/playwright/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class ArtifactsRecorder {
597597
if ((tracing as any)[this._startedCollectingArtifacts])
598598
return;
599599
(tracing as any)[this._startedCollectingArtifacts] = true;
600-
if (this._testInfo._tracing.traceOptions())
600+
if (this._testInfo._tracing.traceOptions() && (tracing as any)[kTracingStarted])
601601
await tracing.stopChunk({ path: this._testInfo._tracing.generateNextTraceRecordingPath() });
602602
}
603603
}

tests/installation/playwright-electron-should-work.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import { test } from './npmTest';
1717
import fs from 'fs';
18+
import { expect } from 'packages/playwright-test';
1819
import path from 'path';
1920

2021
test('electron should work', async ({ exec, tsc, writeFiles }) => {
@@ -39,3 +40,46 @@ test('electron should work with special characters in path', async ({ exec, tmpW
3940
cwd: path.join(folderName)
4041
});
4142
});
43+
44+
test('should work when wrapped inside @playwright/test and trace is enabled', async ({ exec, tmpWorkspace, writeFiles }) => {
45+
await exec('npm i -D @playwright/test electron@31');
46+
await writeFiles({
47+
'electron-with-tracing.spec.ts': `
48+
import { test, expect, _electron } from '@playwright/test';
49+
50+
test('should work', async ({ trace }) => {
51+
const electronApp = await _electron.launch({ args: [${JSON.stringify(path.join(__dirname, '../electron/electron-window-app.js'))}] });
52+
53+
const window = await electronApp.firstWindow();
54+
if (trace)
55+
await window.context().tracing.start({ screenshots: true, snapshots: true });
56+
57+
await window.goto('data:text/html,<title>Playwright</title><h1>Playwright</h1>');
58+
await expect(window).toHaveTitle(/Playwright/);
59+
await expect(window.getByRole('heading')).toHaveText('Playwright');
60+
61+
const path = test.info().outputPath('electron-trace.zip');
62+
if (trace) {
63+
await window.context().tracing.stop({ path });
64+
test.info().attachments.push({ name: 'trace', path, contentType: 'application/zip' });
65+
}
66+
await electronApp.close();
67+
});
68+
`,
69+
});
70+
const jsonOutputName = test.info().outputPath('report.json');
71+
await exec('npx playwright test --trace=on --reporter=json electron-with-tracing.spec.ts', {
72+
env: { PLAYWRIGHT_JSON_OUTPUT_NAME: jsonOutputName }
73+
});
74+
const traces = [
75+
// our actual trace.
76+
path.join(tmpWorkspace, 'test-results', 'electron-with-tracing-should-work', 'electron-trace.zip'),
77+
// contains the expect() calls
78+
path.join(tmpWorkspace, 'test-results', 'electron-with-tracing-should-work', 'trace.zip'),
79+
];
80+
for (const trace of traces)
81+
expect(fs.existsSync(trace)).toBe(true);
82+
const report = JSON.parse(fs.readFileSync(jsonOutputName, 'utf-8'));
83+
expect(new Set(['trace'])).toEqual(new Set(report.suites[0].specs[0].tests[0].results[0].attachments.map(a => a.name)));
84+
expect(new Set(traces.map(p => fs.realpathSync(p)))).toEqual(new Set(report.suites[0].specs[0].tests[0].results[0].attachments.map(a => a.path)));
85+
});

0 commit comments

Comments
 (0)