Created
October 4, 2018 09:14
-
-
Save benzaremean/1d13dbd19e003c33adf37c18802413d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawnSync } = require('child_process'); | |
const os = require('os'); | |
const selenium = require('selenium-standalone'); | |
const chalk = require('chalk'); | |
const chromeVersionAndDriverMapping = { | |
'69': '2.41', | |
'68': '2.41', | |
'67': '2.41', | |
'66': '2.40', | |
'65': '2.38', | |
'64': '2.37', | |
'63': '2.36', | |
'62': '2.35', | |
'61': '2.34', | |
} | |
const getChromeDriverVersion = () => { | |
const DEFAULT_VERSION = '2.41'; | |
if (os.platform() !== 'darwin') { | |
console.warn(chalk.yellow(`Only Darwin platforms are supported at the moment... will default to download chromedriver ${DEFAULT_VERSION}`)); | |
return DEFAULT_VERSION; | |
} | |
try { | |
const chromeBinary = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; | |
const checkChromeVersion = spawnSync(chromeBinary, ['--version']); | |
const regex = new RegExp(/Chrome ([\d]{2})\./); | |
const chromeVersion = !!checkChromeVersion.stdout ? checkChromeVersion.stdout.toString().match(regex)[1] : ''; | |
if (!!chromeVersion && chromeVersionAndDriverMapping[chromeVersion]) { | |
const chromeDriverVersion = chromeVersionAndDriverMapping[chromeVersion]; | |
console.log(chalk.green(`Will attempt downloading Chrome driver version ${chromeDriverVersion} for Chrome version ${chromeVersion}`)); | |
return chromeDriverVersion; | |
} | |
} catch (e) { | |
console.warn(chalk.yellow('Following error was encountered', `\n${e}`)); | |
} | |
return DEFAULT_VERSION; | |
} | |
const seleniumOptions = (chromeVersion) => ({ | |
version: '3.4.0', | |
drivers: { | |
firefox: { | |
version: '0.22.0', | |
arch: process.arch, | |
baseURL: 'https://github.com/mozilla/geckodriver/releases/download' | |
}, | |
chrome: { | |
version: chromeVersion, | |
arch: process.arch, | |
baseURL: 'https://chromedriver.storage.googleapis.com' | |
} | |
}, | |
logger: function (message) { | |
console.log(chalk.green(message)); | |
} | |
}); | |
class SeleniumService { | |
installDependencies(options) { | |
return new Promise((resolve, reject) => { | |
selenium.install(options, (error) => { | |
if (error) { | |
reject(error); | |
} | |
resolve(); | |
}) | |
}); | |
} | |
start(options) { | |
return new Promise((resolve, reject) => { | |
selenium.start(options, (error, process) => { | |
this.process = process; | |
if (error) { | |
reject(error); | |
} | |
resolve(); | |
}) | |
}); | |
} | |
onPrepare() { | |
const chromeDriverVersion = getChromeDriverVersion(); | |
const options = seleniumOptions(chromeDriverVersion); | |
return this.installDependencies(options).then(() => this.start(options)) | |
} | |
onComplete() { | |
if (this.process) { | |
this.process.kill(); | |
} | |
} | |
} | |
module.exports = new SeleniumService(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment