Skip to content

Commit

Permalink
[js] The various driver-specific option classes now extend the Capabi…
Browse files Browse the repository at this point in the history
…lities

class
  • Loading branch information
jleyba committed Dec 21, 2017
1 parent bac9cdf commit 9976795
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 618 deletions.
15 changes: 13 additions & 2 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@ mode.
- Added setChromeService, setEdgeService, & setFirefoxService
- Removed setEnableNativeEvents
- Removed setScrollBehavior
* Changes to `chrome.Options`
- Now extends the `Capabilities` class
- Removed from/toCapabilities
* Changes to `edge.Options`
- Now extends the `Capabilities` class
- Removed from/toCapabilities
* Changes to `ie.Options`
- Now extends the `Capabilities` class
- Removed from/toCapabilities
* Changes to `firefox.Driver`
- Added installAddon(path)
- Added uninstallAddon(id)
* Changes to `firefox.Options`
- Now extends the `Capabilities` class
- Removed from/toCapabilities
- Removed setLoggingPreferences (was a no-op)
- setProfile now only accepts a path to an existing profile
- Added addExtensions
Expand All @@ -82,9 +93,9 @@ mode.
* Removed the `firefox/binary` module
* Removed the `firefox/profile` module
* Changes to `safari.Options`
- Now extends the `Capabilities` class
- Removed from/toCapabilities
- Removed setCleanSession (was a no-op)
- Removed setLoggingPreferences (was a no-op)
- Removed setProxy (was a no-op)
* Changes to `lib/capabilities.Browser`:
- Removed several enum values.
- ANDROID (use Chrome for Android; see docs on the chrome module)
Expand Down
133 changes: 27 additions & 106 deletions javascript/node/selenium-webdriver/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const util = require('util');

const http = require('./http');
const io = require('./io');
const {Capabilities, Capability} = require('./lib/capabilities');
const {Browser, Capabilities, Capability} = require('./lib/capabilities');
const command = require('./lib/command');
const logging = require('./lib/logging');
const promise = require('./lib/promise');
Expand Down Expand Up @@ -324,58 +324,19 @@ const OPTIONS_CAPABILITY_KEY = 'chromeOptions';
/**
* Class for managing ChromeDriver specific options.
*/
class Options {
constructor() {
/** @private {!Object} */
this.options_ = {};

/** @private {!Array<(string|!Buffer)>} */
this.extensions_ = [];

/** @private {?logging.Preferences} */
this.logPrefs_ = null;

/** @private {?./lib/proxy.Config} */
this.proxy_ = null;
}

/**
* Extracts the ChromeDriver specific options from the given capabilities
* object.
* @param {!Capabilities} caps The capabilities object.
* @return {!Options} The ChromeDriver options.
*/
static fromCapabilities(caps) {
let options = new Options();

let o = caps.get(OPTIONS_CAPABILITY_KEY);
if (o instanceof Options) {
options = o;
} else if (o) {
options.
addArguments(o.args || []).
addExtensions(o.extensions || []).
detachDriver(o.detach).
excludeSwitches(o.excludeSwitches || []).
setChromeBinaryPath(o.binary).
setChromeLogFile(o.logPath).
setChromeMinidumpPath(o.minidumpPath).
setLocalState(o.localState).
setMobileEmulation(o.mobileEmulation).
setUserPreferences(o.prefs).
setPerfLoggingPrefs(o.perfLoggingPrefs);
}

if (caps.has(Capability.PROXY)) {
options.setProxy(caps.get(Capability.PROXY));
}
class Options extends Capabilities {
/**
* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
* capabilities to initialize this instance from.
*/
constructor(other = undefined) {
super(other);

if (caps.has(Capability.LOGGING_PREFS)) {
options.setLoggingPrefs(
caps.get(Capability.LOGGING_PREFS));
}
/** @private {!Object} */
this.options_ = this.get(OPTIONS_CAPABILITY_KEY) || {};

return options;
this.setBrowserName(Browser.CHROME);
this.set(OPTIONS_CAPABILITY_KEY, this.options_);
}

/**
Expand Down Expand Up @@ -453,7 +414,8 @@ class Options {
* @return {!Options} A self reference.
*/
addExtensions(...args) {
this.extensions_ = this.extensions_.concat(...args);
let current = this.options_.extensions || [];
this.options_.extensions = current.concat(...args);
return this;
}

Expand Down Expand Up @@ -497,16 +459,6 @@ class Options {
return this;
}

/**
* Sets the logging preferences for the new session.
* @param {!logging.Preferences} prefs The logging preferences.
* @return {!Options} A self reference.
*/
setLoggingPrefs(prefs) {
this.logPrefs_ = prefs;
return this;
}

/**
* Sets the performance logging preferences. Options include:
*
Expand Down Expand Up @@ -684,54 +636,25 @@ class Options {
return this;
}

/**
* Sets the proxy settings for the new session.
* @param {./lib/proxy.Config} proxy The proxy configuration to
* use.
* @return {!Options} A self reference.
*/
setProxy(proxy) {
this.proxy_ = proxy;
return this;
}

/**
* Converts this options instance to a {@link Capabilities} object.
* @param {Capabilities=} opt_capabilities The capabilities to merge
* these options into, if any.
* @return {!Capabilities} The capabilities.
*/
toCapabilities(opt_capabilities) {
let caps = opt_capabilities || Capabilities.chrome();
caps.
set(Capability.PROXY, this.proxy_).
set(Capability.LOGGING_PREFS, this.logPrefs_).
set(OPTIONS_CAPABILITY_KEY, this);
return caps;
}

/**
* Converts this instance to its JSON wire protocol representation. Note this
* function is an implementation not intended for general use.
*
* @return {!Object} The JSON wire protocol representation of this instance.
* @suppress {checkTypes} Suppress [] access on a struct.
*/
[Symbols.serialize]() {
let json = {};
for (let key in this.options_) {
if (this.options_[key] != null) {
json[key] = this.options_[key];
}
}
if (this.extensions_.length) {
json.extensions = this.extensions_.map(function(extension) {
if (Buffer.isBuffer(extension)) {
return extension.toString('base64');
}
return io.read(/** @type {string} */(extension))
.then(buffer => buffer.toString('base64'));
});
if (this.options_.extensions && this.options_.extensions.length) {
this.options_.extensions =
this.options_.extensions.map(function(extension) {
if (Buffer.isBuffer(extension)) {
return extension.toString('base64');
}
return io.read(/** @type {string} */(extension))
.then(buffer => buffer.toString('base64'));
});
}
return json;
return super[Symbols.serialize]();
}
}

Expand Down Expand Up @@ -762,9 +685,7 @@ class Driver extends webdriver.WebDriver {
executor = createExecutor(service.start());
}

let caps =
opt_config instanceof Options ? opt_config.toCapabilities() :
(opt_config || Capabilities.chrome());
let caps = opt_config || Capabilities.chrome();

// W3C spec requires noProxy value to be an array of strings, but Chrome
// expects a single host as a string.
Expand Down
142 changes: 24 additions & 118 deletions javascript/node/selenium-webdriver/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@

'use strict';

const fs = require('fs'),
util = require('util');

const http = require('./http'),
io = require('./io'),
capabilities = require('./lib/capabilities'),
promise = require('./lib/promise'),
Symbols = require('./lib/symbols'),
webdriver = require('./lib/webdriver'),
portprober = require('./net/portprober'),
remote = require('./remote');
const fs = require('fs');
const util = require('util');

const http = require('./http');
const io = require('./io');
const portprober = require('./net/portprober');
const promise = require('./lib/promise');
const remote = require('./remote');
const Symbols = require('./lib/symbols');
const webdriver = require('./lib/webdriver');
const {Browser, Capabilities} = require('./lib/capabilities');

const EDGEDRIVER_EXE = 'MicrosoftWebDriver.exe';

Expand All @@ -97,106 +97,17 @@ function locateSynchronously() {
}


/**
* Option keys.
* @enum {string}
*/
const CAPABILITY_KEY = {
PAGE_LOAD_STRATEGY: 'pageLoadStrategy'
};


/**
* Class for managing MicrosoftEdgeDriver specific options.
*/
class Options {
constructor() {
/** @private {!Object} */
this.options_ = {};

/** @private {?./lib/proxy.Config} */
this.proxy_ = null;
}

/**
* Extracts the MicrosoftEdgeDriver specific options from the given
* capabilities object.
* @param {!capabilities.Capabilities} caps The capabilities object.
* @return {!Options} The MicrosoftEdgeDriver options.
*/
static fromCapabilities(caps) {
var options = new Options();
var map = options.options_;

Object.keys(CAPABILITY_KEY).forEach(function(key) {
key = CAPABILITY_KEY[key];
if (caps.has(key)) {
map[key] = caps.get(key);
}
});

if (caps.has(capabilities.Capability.PROXY)) {
options.setProxy(caps.get(capabilities.Capability.PROXY));
}

return options;
}

class Options extends Capabilities {
/**
* Sets the proxy settings for the new session.
* @param {./lib/proxy.Config} proxy The proxy configuration to use.
* @return {!Options} A self reference.
* @param {(Capabilities|Map<string, ?>|Object)=} other Another set of
* capabilities to initialize this instance from.
*/
setProxy(proxy) {
this.proxy_ = proxy;
return this;
}

/**
* Sets the page load strategy for Edge.
* Supported values are "normal", "eager", and "none";
*
* @param {string} pageLoadStrategy The page load strategy to use.
* @return {!Options} A self reference.
*/
setPageLoadStrategy(pageLoadStrategy) {
this.options_[CAPABILITY_KEY.PAGE_LOAD_STRATEGY] =
pageLoadStrategy.toLowerCase();
return this;
}

/**
* Converts this options instance to a {@link capabilities.Capabilities}
* object.
* @param {capabilities.Capabilities=} opt_capabilities The capabilities to
* merge these options into, if any.
* @return {!capabilities.Capabilities} The capabilities.
*/
toCapabilities(opt_capabilities) {
var caps = opt_capabilities || capabilities.Capabilities.edge();
if (this.proxy_) {
caps.set(capabilities.Capability.PROXY, this.proxy_);
}
Object.keys(this.options_).forEach(function(key) {
caps.set(key, this.options_[key]);
}, this);
return caps;
}

/**
* Converts this instance to its JSON wire protocol representation. Note this
* function is an implementation not intended for general use.
* @return {{pageLoadStrategy: (string|undefined)}}
* The JSON wire protocol representation of this instance.
*/
[Symbols.serialize]() {
var json = {};
for (var key in this.options_) {
if (this.options_[key] != null) {
json[key] = this.options_[key];
}
}
return json;
constructor(other = undefined) {
super(other);
this.setBrowserName(Browser.EDGE);
}
}

Expand Down Expand Up @@ -281,23 +192,18 @@ class Driver extends webdriver.WebDriver {
/**
* Creates a new browser session for Microsoft's Edge browser.
*
* @param {(capabilities.Capabilities|Options)=} opt_config The configuration
* options.
* @param {remote.DriverService=} opt_service The session to use; will use
* @param {(Capabilities|Options)=} options The configuration options.
* @param {remote.DriverService=} service The session to use; will use
* the {@linkplain #getDefaultService default service} by default.
* @return {!Driver} A new driver instance.
*/
static createSession(opt_config, opt_service) {
var service = opt_service || getDefaultService();
var client = service.start().then(url => new http.HttpClient(url));
var executor = new http.Executor(client);

var caps =
opt_config instanceof Options ? opt_config.toCapabilities() :
(opt_config || capabilities.Capabilities.edge());
static createSession(options, service = getDefaultService()) {
let client = service.start().then(url => new http.HttpClient(url));
let executor = new http.Executor(client);

options = options || new Options();
return /** @type {!Driver} */(super.createSession(
executor, caps, () => service.kill()));
executor, options, () => service.kill()));
}

/**
Expand Down
Loading

0 comments on commit 9976795

Please sign in to comment.