Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(tracing): fix tests via tracking context close state (#8906)

Pavel Feldmanaa76cb561053
diff --git a/tests/config/browserTest.ts b/tests/config/browserTest.ts
index 8e77b8d383..3ca7a4c82a 100644
--- a/tests/config/browserTest.ts
+++ b/tests/config/browserTest.ts
@@ -133,9 +133,11 @@ export const playwrightFixtures: Fixtures<PlaywrightTestOptions & PlaywrightTest
},
contextFactory: async ({ browser, contextOptions, trace }, run, testInfo) => {
- const contexts: BrowserContext[] = [];
+ const contexts = new Map<BrowserContext, { closed: boolean }>();
await run(async options => {
const context = await browser.newContext({ ...contextOptions, ...options });
+ contexts.set(context, { closed: false });
+ context.on('close', () => contexts.get(context).closed = true);
if (trace)
await context.tracing.start({ screenshots: true, snapshots: true });
(context as any)._csi = {
@@ -146,12 +148,11 @@ export const playwrightFixtures: Fixtures<PlaywrightTestOptions & PlaywrightTest
return (error?: Error) => newStep?.complete(error);
},
};
- contexts.push(context);
return context;
});
- await Promise.all(contexts.map(async context => {
+ await Promise.all([...contexts.keys()].map(async context => {
const videos = context.pages().map(p => p.video()).filter(Boolean);
- if (!(context as any)._closed && trace) {
+ if (trace && !contexts.get(context)!.closed) {
const tracePath = testInfo.outputPath('trace.zip');
await context.tracing.stop({ path: tracePath });
testInfo.attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip' });