From 844ecbe2eb494fd82af37e42eb8168210df8eaa2 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 5 Apr 2022 18:45:21 +0200 Subject: [PATCH] chore: better Electron error message when no executable path is given (#13318) --- .../src/server/electron/electron.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/server/electron/electron.ts b/packages/playwright-core/src/server/electron/electron.ts index 35d4c49ff9..8b6dac8822 100644 --- a/packages/playwright-core/src/server/electron/electron.ts +++ b/packages/playwright-core/src/server/electron/electron.ts @@ -24,6 +24,7 @@ import { CRExecutionContext } from '../chromium/crExecutionContext'; import * as js from '../javascript'; import { Page } from '../page'; import { TimeoutSettings } from '../../utils/timeoutSettings'; +import { wrapInASCIIBox } from '../../utils/utils'; import { WebSocketTransport } from '../transport'; import { launchProcess, envArrayToObject } from '../../utils/processLauncher'; import { BrowserContext, validateBrowserContextOptions } from '../browserContext'; @@ -134,12 +135,32 @@ export class Electron extends SdkObject { const browserLogsCollector = new RecentLogsCollector(); const env = options.env ? envArrayToObject(options.env) : process.env; + + let command: string; + if (options.executablePath) { + command = options.executablePath; + } else { + try { + // By default we fallback to the Electron App executable path. + // 'electron/index.js' resolves to the actual Electron App. + command = require('electron/index.js'); + } catch (error: any) { + if ((error as NodeJS.ErrnoException)?.code === 'MODULE_NOT_FOUND') { + throw new Error('\n' + wrapInASCIIBox([ + 'Electron executablePath not found!', + 'Please install it using `npm install -D electron` or set the executablePath to your Electron executable.', + ].join('\n'), 1)); + } + throw error; + } + } + // When debugging Playwright test that runs Electron, NODE_OPTIONS // will make the debugger attach to Electron's Node. But Playwright // also needs to attach to drive the automation. Disable external debugging. delete env.NODE_OPTIONS; const { launchedProcess, gracefullyClose, kill } = await launchProcess({ - command: options.executablePath || require('electron/index.js'), + command, args: electronArguments, env, log: (message: string) => {