Unverified Commit 61889f89 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

fix(playwright): teardown when global quit force terminates browser (#59548)

parent ed12b552
...@@ -555,7 +555,8 @@ ${ENDGROUP}`) ...@@ -555,7 +555,8 @@ ${ENDGROUP}`)
// If environment is CI and if this test execution is failed after retry, preserve test traces // If environment is CI and if this test execution is failed after retry, preserve test traces
// to upload into github actions artifacts for debugging purpose // to upload into github actions artifacts for debugging purpose
const shouldPreserveTracesOutput = const shouldPreserveTracesOutput =
process.env.CI && isRetry && isChildExitWithNonZero (process.env.CI && isRetry && isChildExitWithNonZero) ||
process.env.PRESERVE_TRACES_OUTPUT
if (!shouldPreserveTracesOutput) { if (!shouldPreserveTracesOutput) {
await fsp await fsp
.rm( .rm(
......
...@@ -21,13 +21,25 @@ let websocketFrames: Array<{ payload: string | Buffer }> = [] ...@@ -21,13 +21,25 @@ let websocketFrames: Array<{ payload: string | Buffer }> = []
const tracePlaywright = process.env.TRACE_PLAYWRIGHT const tracePlaywright = process.env.TRACE_PLAYWRIGHT
// loose global to register teardown functions before quitting the browser instance.
// This is due to `quit` can be called anytime outside of BrowserInterface's lifecycle,
// which can create corrupted state by terminating the context.
// [TODO] global `quit` might need to be removed, instead should introduce per-instance teardown
const pendingTeardown = []
export async function quit() { export async function quit() {
await Promise.all(pendingTeardown.map((fn) => fn()))
await context?.close() await context?.close()
await browser?.close() await browser?.close()
context = undefined context = undefined
browser = undefined browser = undefined
} }
async function teardown(tearDownFn: () => Promise<void>) {
pendingTeardown.push(tearDownFn)
await tearDownFn()
pendingTeardown.splice(pendingTeardown.indexOf(tearDownFn), 1)
}
interface ElementHandleExt extends ElementHandle { interface ElementHandleExt extends ElementHandle {
getComputedCss(prop: string): Promise<string> getComputedCss(prop: string): Promise<string>
text(): Promise<string> text(): Promise<string>
...@@ -38,7 +50,6 @@ export class Playwright extends BrowserInterface { ...@@ -38,7 +50,6 @@ export class Playwright extends BrowserInterface {
private eventCallbacks: Record<Event, Set<(...args: any[]) => void>> = { private eventCallbacks: Record<Event, Set<(...args: any[]) => void>> = {
request: new Set(), request: new Set(),
} }
private async initContextTracing( private async initContextTracing(
url: string, url: string,
page: Page, page: Page,
...@@ -50,7 +61,7 @@ export class Playwright extends BrowserInterface { ...@@ -50,7 +61,7 @@ export class Playwright extends BrowserInterface {
try { try {
// Clean up if any previous traces are still active // Clean up if any previous traces are still active
await this.teardownTracing() await teardown(this.teardownTracing.bind(this))
await context.tracing.start({ await context.tracing.start({
screenshots: true, screenshots: true,
...@@ -60,7 +71,7 @@ export class Playwright extends BrowserInterface { ...@@ -60,7 +71,7 @@ export class Playwright extends BrowserInterface {
this.activeTrace = encodeURIComponent(url) this.activeTrace = encodeURIComponent(url)
page.on('close', async () => { page.on('close', async () => {
await this.teardownTracing() await teardown(this.teardownTracing.bind(this))
}) })
} catch (e) { } catch (e) {
this.activeTrace = undefined this.activeTrace = undefined
...@@ -87,6 +98,7 @@ export class Playwright extends BrowserInterface { ...@@ -87,6 +98,7 @@ export class Playwright extends BrowserInterface {
path: traceOutputPath, path: traceOutputPath,
}) })
} catch (e) { } catch (e) {
require('console').warn('Failed to teardown playwright tracing', e)
} finally { } finally {
this.activeTrace = undefined this.activeTrace = undefined
} }
...@@ -128,6 +140,7 @@ export class Playwright extends BrowserInterface { ...@@ -128,6 +140,7 @@ export class Playwright extends BrowserInterface {
if (browser) { if (browser) {
if (contextHasJSEnabled !== javaScriptEnabled) { if (contextHasJSEnabled !== javaScriptEnabled) {
// If we have switched from having JS enable/disabled we need to recreate the context. // If we have switched from having JS enable/disabled we need to recreate the context.
await teardown(this.teardownTracing.bind(this))
await context?.close() await context?.close()
context = await browser.newContext({ context = await browser.newContext({
locale, locale,
...@@ -135,9 +148,6 @@ export class Playwright extends BrowserInterface { ...@@ -135,9 +148,6 @@ export class Playwright extends BrowserInterface {
ignoreHTTPSErrors, ignoreHTTPSErrors,
...device, ...device,
}) })
context.once('close', async () => {
await this.teardownTracing()
})
contextHasJSEnabled = javaScriptEnabled contextHasJSEnabled = javaScriptEnabled
} }
return return
...@@ -150,9 +160,6 @@ export class Playwright extends BrowserInterface { ...@@ -150,9 +160,6 @@ export class Playwright extends BrowserInterface {
ignoreHTTPSErrors, ignoreHTTPSErrors,
...device, ...device,
}) })
context.once('close', async () => {
await this.teardownTracing()
})
contextHasJSEnabled = javaScriptEnabled contextHasJSEnabled = javaScriptEnabled
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment