Skip to content

Commit

Permalink
feat: Automatically fetch and install drivers
Browse files Browse the repository at this point in the history
Drivers are automatically fetched and installed to a cache in your
home directory.  They will always match the local browser version,
without the need for any manual intervention.  This is done using the
webdriver-installer module (also by me).
  • Loading branch information
joeyparrish committed Jan 31, 2022
1 parent afa5117 commit 1e7a0ca
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ npm install --save-dev karma-local-wd-launcher

## Drivers

This launcher assumes that drivers such as `chromedriver`, `geckodriver`, and
`safaridriver` (Mac only) are already installed, executable, and in your `PATH`.
This launcher will use the
[`webdriver-installer`](https://www.npmjs.com/package/webdriver-installer)
module to automatically download and install driver binaries for your local
browsers. These will be cached in a folder called `.webdriver-installer-cache`
in your home directory.

Alternately, you may use the environment variables `CHROMEDRIVER_PATH`,
`GECKODRIVER_PATH`, or `SAFARIDRIVER_PATH` to indicate where these drivers are
installed.
already installed.


## Configuration
Expand Down
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
* Supports Chrome, Firefox, and Safari.
*/

const fs = require('fs');
const os = require('os');
const path = require('path');
const url = require('url');
const wd = require('wd');

const {installWebDrivers} = require('webdriver-installer');

const DRIVER_CACHE = path.join(os.homedir(), '.webdriver-installer-cache');
fs.mkdirSync(DRIVER_CACHE, {recursive: true});

let driversInstalledPromise = null;

// Map nodejs OS names to Selenium platform names.
const PLATFORM_MAP = {
'darwin': 'Mac',
Expand All @@ -32,6 +41,14 @@ const LocalWebDriverBase = function(

this.browserName = browserName;

if (driverCommand[0] == '/') {
// Absolute path. Keep it.
} else {
// File name. Assume it's in our driver cache.
driverCommand = path.join(DRIVER_CACHE, driverCommand);
}

// Checked by the base class to determine what command to run.
this.DEFAULT_CMD = {
linux: driverCommand,
darwin: driverCommand,
Expand All @@ -43,7 +60,8 @@ const LocalWebDriverBase = function(
// Called by the base class to get arguments to pass to the driver command.
this._getOptions = () => argsFromPort(port.toString());

this.ENV_CMD = driverCommand.toUpperCase() + '_PATH';
// An environment variable that can be used to override the command path.
this.ENV_CMD = driverCommand.toUpperCase().replace('-', '_') + '_PATH';

const config = {
protocol: 'http:',
Expand Down Expand Up @@ -110,7 +128,18 @@ const LocalWebDriverBase = function(
const originalStart = this.start;
let previousUrl = null;

this.start = (url) => {
this.start = async (url) => {
// If we haven't installed drivers yet in this session, start the
// installation process now.
if (!driversInstalledPromise) {
// TODO: Tie logging for this to karma log settings.
driversInstalledPromise =
installWebDrivers(DRIVER_CACHE, /* logging= */ false);
}

// Wait for drivers to be installed for all local browsers.
await driversInstalledPromise;

previousUrl = url;
originalStart.call(this, url);
};
Expand Down
132 changes: 131 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"author": "Joey Parrish <[email protected]>",
"dependencies": {
"wd": "^1.14.0"
"wd": "^1.14.0",
"webdriver-installer": "^1.0.0"
},
"peerDependencies": {
"karma": "^6.2.0"
Expand Down

0 comments on commit 1e7a0ca

Please sign in to comment.