Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Last active April 12, 2017 06:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thom-nic/84f529dd0a2f446043dff6fac6256d1c to your computer and use it in GitHub Desktop.
Save thom-nic/84f529dd0a2f446043dff6fac6256d1c to your computer and use it in GitHub Desktop.
Chromedriver auto-start service for WebdriverIO without Selenium
const chromedriver = require('chromedriver');
const retry = require('p-retry');
const http = require('axios');
/**
* Start chromedriver and waits for it to become available before the tests start.
* Call this on wdio.confg `onPrepare`, passing `this` (the wdio config)
*/
function start(config) {
const pollUrl = `http://${config.host || 'localhost'}:${config.port || 4444}${config.path || '/wd/hub'}/status`;
console.log('chromedriver starting on:', pollUrl);
const proc = chromedriver.start([`--url-base=${config.path ||'/wd/hub'}`, `--port=${config.port||4444}`]);
process.on('exit', () => proc.kill()); // attempt to cleanup this subprocess
return retry((() => http.get(pollUrl)), {retries:5, minDelay: 200});
}
/**
* Call this on wdio.config `after` hook. May not strictly be necessary
* because the child process should be killed when this test exits.
*/
function stop() {
chromedriver.stop();
}
module.exports = {
start,
stop
}
exports.config = {
// add other options here as normal
capabilities: [
{ browserName: 'chrome'},
],
// This is the important part!!!
onPrepare: () => {
if ( this.config.chromeDriver ) require('./chromedriver').start(this.config);
},
after: () => {
if ( this.config.chromeDriver ) require('./chromedriver').stop();
},
// custom option:
chromeDriver: true,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment