From 5b7d6252b0492c98a1cc85036adbdfe061b9f247 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 29 Jul 2020 16:38:59 -0700 Subject: [PATCH] fix: a pretty error when browser executable is not found Make sure executable exists before launching it. If it doesn't and we were launched without custom executable path, print a helpful instruction to run `npm i playwright` and get browsers downloaded. Note: there's already a test that makes sure bad executable paths are treated fairly: https://github.com/microsoft/playwright/blob/9132d23b2bfadfcf46461f2135c37d9689d91033/test/launcher.jest.js#L54-L59 This doesn't test missing default browser installation which I think is fine. Fixes #3161 --- src/server/browserType.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/server/browserType.ts b/src/server/browserType.ts index afae874635188..a6a2ebf7ced6d 100644 --- a/src/server/browserType.ts +++ b/src/server/browserType.ts @@ -55,6 +55,7 @@ export interface BrowserType { const mkdirAsync = util.promisify(fs.mkdir); const mkdtempAsync = util.promisify(fs.mkdtemp); +const existsAsync = (path: string): Promise => new Promise(resolve => fs.stat(path, err => resolve(!err))); const DOWNLOADS_FOLDER = path.join(os.tmpdir(), 'playwright_downloads-'); type WebSocketNotPipe = { webSocketRegex: RegExp, stream: 'stdout' | 'stderr' }; @@ -190,6 +191,13 @@ export abstract class BrowserTypeBase implements BrowserType { const executable = executablePath || this.executablePath(); if (!executable) throw new Error(`No executable path is specified. Pass "executablePath" option directly.`); + if (!(await existsAsync(executable))) { + const errorMessageLines = [`Failed to launch ${this._name} because executable doesn't exist at ${executable}`]; + // If we tried using stock downloaded browser, suggest re-installing playwright. + if (!executablePath) + errorMessageLines.push(`Try re-installing playwright with "npm install playwright"`); + throw new Error(errorMessageLines.join('\n')); + } if (!executablePath) { // We can only validate dependencies for bundled browsers.