Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChromeDriver should fail fast if child process exits of its own accord #852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 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,32 @@ 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 (!this.process) {
throw new Error('ChromeDriver has been stopped');
}

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

if (isRunning) {
return;
}

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

await delay(100);
}
};

ChromeDriver.prototype.setupLogs = function () {
Expand Down Expand Up @@ -120,12 +121,14 @@ 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 = async function () {
try {
const { value } = await got(this.statusUrl).json();
return value && value.ready;
} catch (err) {
console.log('err', err);
return false;
}
};

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