Skip to content

Commit

Permalink
feat: Customize WebDriver config in custom launcher (shaka-project#35)
Browse files Browse the repository at this point in the history
Example of custom launcher with this PR:

module.exports = function(config) {
  const firefoxOptions = {
    'moz:firefoxOptions': {
      prefs: {
        'media.eme.enabled': true,
        'media.gmp-manager.updateEnabled': true,
      },
    },
  };

  config.set({
    customLaunchers: {
      'FirefoxEME': {
        base: 'Firefox',
        config: firefoxOptions,
      },
      'FirefoxHeadlessEME': {
        base: 'FirefoxHeadless',
        config: firefoxOptions,
      },
    },
  });
};
  • Loading branch information
tykus160 authored Mar 23, 2022
1 parent 4e6e745 commit c8fe569
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ advanced tests to be executed in-browser. If you don't need WebDriver to
enable some test scenario in Karma, you can just use typical local browser
launchers.

Supports Chrome, Firefox, and Safari.
Supports Chrome, Firefox, Edge, and Safari.


## Installation
Expand Down Expand Up @@ -36,7 +36,8 @@ already installed.
// karma.conf.js
module.exports = (config) => {
config.set({
browsers: ['Chrome', 'Firefox', 'Safari']
plugins: ['karma-local-wd-launcher'],
browsers: ['Chrome', 'Firefox', 'Edge', 'Safari'],
});
};
```
Expand All @@ -46,3 +47,28 @@ You can give Karma's command-line interface a list of browsers, too:
```sh
karma start --browsers Chrome Firefox Safari
```

There is a possibilty to create custom launcher based on existing ones, i.e. to
pass additional configuration options to specific WebDriver.

```js
// karma.conf.js
module.exports = (config) => {
config.set({
customLaunchers: {
'ChromeNoBackgroundSuspend': {
base: 'Chrome',
config: {
'goog:chromeOptions': {
args: [
'--disable-background-media-suspend',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
],
},
},
},
},
});
};
```
29 changes: 23 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const wd = require('wd');
const _ = require('lodash');

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

Expand All @@ -39,7 +40,7 @@ const PLATFORM_MAP = {
// - LAUNCHER_NAME: launcher name as presented to Karma
// - EXTRA_WEBDRIVER_SPECS: an object containing any extra WebDriver specs
// - getDriverArgs(port): take port as string, return driver command arguments
const LocalWebDriverBase = function(baseBrowserDecorator, logger) {
const LocalWebDriverBase = function(baseBrowserDecorator, args, logger) {
baseBrowserDecorator(this);

this.name = `${this.constructor.LAUNCHER_NAME} via WebDriver`;
Expand All @@ -61,6 +62,10 @@ const LocalWebDriverBase = function(baseBrowserDecorator, logger) {

log.debug('config:', JSON.stringify(config));

const extraSpecs =
_.merge(this.constructor.EXTRA_WEBDRIVER_SPECS, args.config);
log.debug('extraSpecs:', extraSpecs);

// These names ("browser" and "spec") are needed for compatibility with
// karma-webdriver-launcher.
this.browser = wd.remote(config);
Expand All @@ -70,7 +75,7 @@ const LocalWebDriverBase = function(baseBrowserDecorator, logger) {
// This is necessary for safaridriver:
allowW3C: true,
// This allows extra configuration for headless variants:
...this.constructor.EXTRA_WEBDRIVER_SPECS,
...extraSpecs,
};

this.browser.on('status', (info) => {
Expand Down Expand Up @@ -192,8 +197,8 @@ function generateSubclass(

// Karma will not use "new" to construct our class, so it can't be a true ES6
// class. Use the old function syntax instead.
const subclass = function(baseBrowserDecorator, logger) {
LocalWebDriverBase.call(this, baseBrowserDecorator, logger);
const subclass = function(baseBrowserDecorator, args, logger) {
LocalWebDriverBase.call(this, baseBrowserDecorator, args, logger);
};

// These are needed by our base class, LocalWebDriverBase.
Expand All @@ -215,7 +220,7 @@ function generateSubclass(
};

// This configures Karma's dependency injection system:
subclass.$inject = ['baseBrowserDecorator', 'logger'];
subclass.$inject = ['baseBrowserDecorator', 'args', 'logger'];

return subclass;
}
Expand Down Expand Up @@ -274,14 +279,26 @@ const LocalWebDriverEdgeHeadless = generateSubclass(
const LocalWebDriverFirefox = generateSubclass(
'Firefox', 'Firefox',
'geckodriver',
(port) => ['-p', port]);
(port) => ['-p', port],
{
'moz:firefoxOptions': {
prefs: {
'media.eme.enabled': true,
'media.gmp-manager.updateEnabled': true,
},
},
});

const LocalWebDriverFirefoxHeadless = generateSubclass(
'Firefox', 'FirefoxHeadless',
'geckodriver',
(port) => ['-p', port],
{
'moz:firefoxOptions': {
prefs: {
'media.eme.enabled': true,
'media.gmp-manager.updateEnabled': true,
},
args: [
'-headless',
],
Expand Down

0 comments on commit c8fe569

Please sign in to comment.