Skip to content

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

fix(electron): do not drop windows that have not navigated while connecting (#43250)

Pavel Feldmaneae2e3f5005a
diff --git a/packages/playwright-core/src/server/browser.ts b/packages/playwright-core/src/server/browser.ts
index b244689351..a1fea9f2df 100644
--- a/packages/playwright-core/src/server/browser.ts
+++ b/packages/playwright-core/src/server/browser.ts
@@ -66,6 +66,8 @@ export type BrowserOptions = {
userDataDir?: string;
noDefaults?: boolean;
isWebView?: boolean;
+ // Do not send Inspector.enable to detect pages without a renderer while connecting.
+ noInspectorEnableOnConnect?: boolean;
};
export abstract class Browser extends SdkObject {
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index 95c650e37d..1510bd07df 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -576,7 +576,7 @@ class FrameSession {
for (const initScript of this._crPage._page.allInitScripts())
promises.push(this._evaluateOnNewDocument(initScript, 'main', true /* runImmediately */));
}
- if (this._isMainFrame() && this._crPage._browserContext._browser._isConnecting) {
+ if (this._isMainFrame() && this._crPage._browserContext._browser._isConnecting && !browserOptions.noInspectorEnableOnConnect) {
// An existing page without a renderer, e.g. crashed or discarded, never responds to the commands above.
// Get notified with Inspector.targetCrashed right away, so that such a page is reported as closed,
// and we do not stall while connecting to the browser.
diff --git a/packages/playwright-core/src/server/electron/electron.ts b/packages/playwright-core/src/server/electron/electron.ts
index 7e3d3b01b1..a13e4f42f2 100644
--- a/packages/playwright-core/src/server/electron/electron.ts
+++ b/packages/playwright-core/src/server/electron/electron.ts
@@ -307,6 +307,8 @@ export class Electron extends SdkObject {
downloadsPath: artifactsDir,
tracesDir: options.tracesDir || artifactsDir,
originalLaunchOptions: {},
+ // A window that has not navigated yet has no renderer, but it is not crashed.
+ noInspectorEnableOnConnect: true,
};
validateBrowserContextOptions(contextOptions, browserOptions);
const browser = await progress.race(CRBrowser.connect(this.attribution.playwright, chromeTransport, browserOptions));
diff --git a/tests/electron/electron-app.spec.ts b/tests/electron/electron-app.spec.ts
index adbca5e7fe..05749b971e 100644
--- a/tests/electron/electron-app.spec.ts
+++ b/tests/electron/electron-app.spec.ts
@@ -334,6 +334,16 @@ test('should be able to get the first window when with a delayed navigation', as
await expect(page.locator('h1')).toHaveText('Foobar');
});
+test('should be able to get the first window with a delayed navigation when launched via executablePath', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43247' },
+}, async ({ launchElectronApp }) => {
+ // Like a packaged app: without our loader, the window is created while Playwright is still connecting.
+ const app = await launchElectronApp('electron-window-app-delayed-loadURL.js', [], { executablePath: require('electron/index.js') });
+ const page = await app.firstWindow();
+ await expect(page).toHaveURL('data:text/html,<h1>Foobar</h1>');
+ await expect(page.locator('h1')).toHaveText('Foobar');
+});
+
test('should detach debugger on app-initiated exit', async ({ launchElectronApp }) => {
const electronApp = await launchElectronApp('electron-app.js');
const closePromise = new Promise(f => electronApp.process().on('close', f));