diff --git a/packages/playwright-core/src/server/electron/loader.ts b/packages/playwright-core/src/server/electron/loader.ts index b1443751b3..4fe1759b40 100644 --- a/packages/playwright-core/src/server/electron/loader.ts +++ b/packages/playwright-core/src/server/electron/loader.ts @@ -31,27 +31,30 @@ for (const arg of chromiumSwitches) { app.commandLine.appendSwitch(match[1], match[2]); } +// Defer ready event. +const originalWhenReady = app.whenReady(); +const originalEmit = app.emit.bind(app); +let readyEventArgs: any[]; +app.emit = (event: string | symbol, ...args: any[]): boolean => { + if (event === 'ready') { + readyEventArgs = args; + return app.listenerCount('ready') > 0; + } + return originalEmit(event, ...args); +}; app.getAppPath = () => path.dirname(appPath); +let isReady = false; +let whenReadyCallback: (event: any) => any; +const whenReadyPromise = new Promise(f => whenReadyCallback = f); +app.isReady = () => isReady; +app.whenReady = () => whenReadyPromise; -let launchInfoEventPayload: any; -app.on('ready', launchInfo => launchInfoEventPayload = launchInfo); +require(appPath); (globalThis as any).__playwright_run = async () => { // Wait for app to be ready to avoid browser initialization races. - await app.whenReady(); - - // Override isReady pipeline. - let isReady = false; - let whenReadyCallback: () => void; - const whenReadyPromise = new Promise(f => whenReadyCallback = f); - app.isReady = () => isReady; - app.whenReady = () => whenReadyPromise; - - require(appPath); - - // Trigger isReady. + const event = await originalWhenReady; isReady = true; - whenReadyCallback!(); - app.emit('will-finish-launching'); - app.emit('ready', launchInfoEventPayload); + whenReadyCallback(event); + originalEmit('ready', ...readyEventArgs); }; diff --git a/tests/electron/electron-app.js b/tests/electron/electron-app.js index 0e68871199..d8ea432c56 100644 --- a/tests/electron/electron-app.js +++ b/tests/electron/electron-app.js @@ -1,6 +1,9 @@ const { app, protocol } = require('electron'); const path = require('path'); +// Test using pre-ready apis. +protocol.registerSchemesAsPrivileged([]); + app.on('window-all-closed', e => e.preventDefault()); app.whenReady().then(() => {