Skip to content

Commit

Permalink
ChromeDriver should fail fast if child process exits of its own accord
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokel81 committed Feb 8, 2021
1 parent f4d097a commit dc67289
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions lib/chrome-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const path = require('path');
const { default: got } = require('got');
const split = require('split');

function delay(duration) {
return new Promise(resolve => global.setTimeout(resolve, duration));
}

function ChromeDriver(
host,
port,
Expand All @@ -20,19 +24,18 @@ function ChromeDriver(

this.path = require.resolve('electron-chromedriver/chromedriver');
this.urlBase = '/';
this.statusUrl =
'http://' + this.host + ':' + this.port + this.urlBase + 'status';
this.statusUrl = `http://${this.host}:${this.port}${this.urlBase}status`;
this.logLines = [];
}

ChromeDriver.prototype.start = function () {
if (this.process) throw new Error('ChromeDriver already started');

const args = [this.path, '--port=' + this.port, '--url-base=' + this.urlBase];
const args = [this.path, `--port=${this.port}`, `--url-base=${this.urlBase}`];

if (this.chromeDriverLogPath) {
args.push('--verbose');
args.push('--log-path=' + this.chromeDriverLogPath);
args.push(`--log-path=${this.chromeDriverLogPath}`);
}
const options = {
cwd: this.workingDirectory,
Expand All @@ -50,34 +53,30 @@ ChromeDriver.prototype.start = function () {
return this.waitUntilRunning();
};

ChromeDriver.prototype.waitUntilRunning = function () {
const self = this;
return new Promise(function (resolve, reject) {
const startTime = Date.now();
const checkIfRunning = function () {
self.isRunning(function (running) {
if (!self.process) {
return reject(Error('ChromeDriver has been stopped'));
}

if (running) {
return resolve();
}

const elapsedTime = Date.now() - startTime;
if (elapsedTime > self.startTimeout) {
return reject(
Error(
'ChromeDriver did not start within ' + self.startTimeout + 'ms'
)
);
}

global.setTimeout(checkIfRunning, 100);
});
};
checkIfRunning();
});
ChromeDriver.prototype.waitUntilRunning = async function () {
const startTime = Date.now();
for (;;) {
const isRunning = await this.isRunning();

if (!self.process) {
throw new Error('ChromeDriver has been stopped');
}

if (self.process.exitCode !== null) {
throw new Error(`ChromeDriver exited with code ${self.process.exitCode}`);
}

if (isRunning) {
return;
}

const elapsedTime = Date.now() - startTime;
if (elapsedTime > self.startTimeout) {
throw new Error(`ChromeDriver did not start within ${self.startTimeout}ms`);
}

await delay(100);
}
};

ChromeDriver.prototype.setupLogs = function () {
Expand Down Expand Up @@ -120,12 +119,13 @@ ChromeDriver.prototype.stop = function () {
this.clearLogs();
};

ChromeDriver.prototype.isRunning = function (callback) {
const cb = false;
got(this.statusUrl)
.json()
.then(({ value }) => callback(value && value.ready))
.catch(() => callback(cb));
ChromeDriver.prototype.isRunning = function () {
try {
const { value } = got(this.statusUrl).json();
return value && value.ready;
} catch (_) {
return false;
}
};

ChromeDriver.prototype.getLogs = function () {
Expand Down

0 comments on commit dc67289

Please sign in to comment.