diff --git a/lib/Browser.js b/lib/Browser.js index 7ed79781..e6452462 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -99,6 +99,8 @@ class Browser { if (this._chromeProcess) return; this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {}); + var stderr = ''; + this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8')); // Cleanup as processes exit. process.on('exit', () => this._chromeProcess.kill()); this._chromeProcess.on('exit', () => { @@ -106,7 +108,9 @@ class Browser { removeRecursive(this._userDataDir); }); - await waitForChromeResponsive(this._remoteDebuggingPort); + await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated); + if (this._terminated) + throw new Error('Failed to launch chrome! ' + stderr); } close() { @@ -118,7 +122,7 @@ class Browser { module.exports = Browser; -function waitForChromeResponsive(remoteDebuggingPort) { +function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) { var fulfill; var promise = new Promise(x => fulfill = x); var options = { @@ -135,7 +139,12 @@ function waitForChromeResponsive(remoteDebuggingPort) { var req = http.request(options, res => { fulfill(); }); - req.on('error', e => setTimeout(sendRequest, probeTimeout)); + req.on('error', e => { + if (shouldWaitCallback()) + setTimeout(sendRequest, probeTimeout); + else + fulfill(); + }); req.end(); } }