Skip to content

Commit

Permalink
[js] Improve integration with ES6 modules and type checking with the …
Browse files Browse the repository at this point in the history
…closure

compiler.
  • Loading branch information
jleyba committed Mar 14, 2016
1 parent 8527ced commit 9bfdbb3
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 143 deletions.
6 changes: 4 additions & 2 deletions javascript/node/selenium-webdriver/lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,5 +590,7 @@ class TouchSequence {

// PUBLIC API

exports.ActionSequence = ActionSequence;
exports.TouchSequence = TouchSequence;
module.exports = {
ActionSequence: ActionSequence,
TouchSequence: TouchSequence,
};
7 changes: 4 additions & 3 deletions javascript/node/selenium-webdriver/lib/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function check(locator) {

// PUBLIC API


exports.By = By;
exports.checkedLocator = check;
module.exports = {
By: By,
checkedLocator: check,
};
96 changes: 71 additions & 25 deletions javascript/node/selenium-webdriver/lib/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,48 @@ const Capability = {

/**
* Describes how a proxy should be configured for a WebDriver session.
* Proxy configuration object, as defined by the WebDriver wire protocol.
* @typedef {(
* {proxyType: string}|
* {proxyType: string,
* proxyAutoconfigUrl: string}|
* {proxyType: string,
* ftpProxy: string,
* httpProxy: string,
* sslProxy: string,
* noProxy: string})}
* @record
*/
var ProxyConfig;
function ProxyConfig() {}

/**
* The proxy type. Must be one of {"manual", "pac", "system"}.
* @type {string}
*/
ProxyConfig.prototype.proxyType;

/**
* URL for the PAC file to use. Only used if {@link #proxyType} is "pac".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.proxyAutoconfigUrl;

/**
* The proxy host for FTP requests. Only used if {@link #proxyType} is "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.ftpProxy;

/**
* The proxy host for HTTP requests. Only used if {@link #proxyType} is
* "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.httpProxy;

/**
* The proxy host for HTTPS requests. Only used if {@link #proxyType} is
* "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.sslProxy;

/**
* A comma delimited list of hosts which should bypass all proxies. Only used if
* {@link #proxyType} is "manual".
* @type {(string|undefined)}
*/
ProxyConfig.prototype.noProxy;


/**
Expand Down Expand Up @@ -291,16 +321,11 @@ class Capabilities extends Map {
/**
* @return {!Object<string, ?>} The JSON representation of this instance.
* Note, the returned object may contain nested promised values.
* @suppress {checkTypes} Suppress [] access on a struct (state inherited from
* Map).
*/
[Symbols.serialize]() {
let ret = {};
for (let key of this.keys()) {
let cap = this.get(key);
if (cap) {
ret[key] = cap;
}
}
return ret;
return serialize(this);
}

/**
Expand Down Expand Up @@ -393,12 +418,33 @@ class Capabilities extends Map {
}


// PUBLIC API
/**
* Serializes a capabilities object. This is defined as a standalone function
* so it may be type checked (where Capabilities[Symbols.serialize] has type
* checking disabled since it is defined with [] access on a struct).
*
* @param {!Capabilities} caps The capabilities to serialize.
* @return {!Object<string, ?>} The JSON representation of this instance.
* Note, the returned object may contain nested promised values.
*/
function serialize(caps) {
let ret = {};
for (let key of caps.keys()) {
let cap = caps.get(key);
if (cap) {
ret[key] = cap;
}
}
return ret;
}


exports.Browser = Browser;
exports.Capabilities = Capabilities;
exports.Capability = Capability;
// PUBLIC API


/** @typedef {ProxyConfig} */
exports.ProxyConfig = ProxyConfig;
module.exports = {
Browser: Browser,
Capabilities: Capabilities,
Capability: Capability,
ProxyConfig: ProxyConfig
};
10 changes: 6 additions & 4 deletions javascript/node/selenium-webdriver/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ class DeferredExecutor {
// PUBLIC API


exports.Command = Command;
exports.Name = Name;
exports.Executor = Executor;
exports.DeferredExecutor = DeferredExecutor;
module.exports = {
Command: Command,
Name: Name,
Executor: Executor,
DeferredExecutor: DeferredExecutor
};
94 changes: 62 additions & 32 deletions javascript/node/selenium-webdriver/lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,33 @@ const ERROR_CODE_TO_TYPE = new Map([
['unsupported operation', UnsupportedOperationError]]);


const TYPE_TO_ERROR_CODE = new Map;
ERROR_CODE_TO_TYPE.forEach((value, key) => {
TYPE_TO_ERROR_CODE.set(value, key);
});



/**
* @param {*} err The error to encode.
* @return {{error: string, message: string}} the encoded error.
*/
function encodeError(err) {
let type = WebDriverError;
if (err instanceof WebDriverError
&& TYPE_TO_ERROR_CODE.has(err.constructor)) {
type = err.constructor;
}

let message = err instanceof Error
? err.message
: err + '';

let code = /** @type {string} */(TYPE_TO_ERROR_CODE.get(type));
return {'error': code, 'message': message};
}


/**
* Checks a response object from a server that adheres to the W3C WebDriver
* protocol.
Expand Down Expand Up @@ -626,35 +653,38 @@ function checkLegacyResponse(responseObj) {
// PUBLIC API


exports.ErrorCode = ErrorCode;

exports.WebDriverError = WebDriverError;
exports.ElementNotSelectableError = ElementNotSelectableError;
exports.ElementNotVisibleError = ElementNotVisibleError;
exports.InvalidArgumentError = InvalidArgumentError;
exports.InvalidCookieDomainError = InvalidCookieDomainError;
exports.InvalidElementCoordinatesError = InvalidElementCoordinatesError;
exports.InvalidElementStateError = InvalidElementStateError;
exports.InvalidSelectorError = InvalidSelectorError;
exports.InvalidSessionIdError = InvalidSessionIdError;
exports.JavascriptError = JavascriptError;
exports.MoveTargetOutOfBoundsError = MoveTargetOutOfBoundsError;
exports.NoSuchAlertError = NoSuchAlertError;
exports.NoSuchElementError = NoSuchElementError;
exports.NoSuchFrameError = NoSuchFrameError;
exports.NoSuchSessionError = NoSuchSessionError;
exports.NoSuchWindowError = NoSuchWindowError;
exports.ScriptTimeoutError = ScriptTimeoutError;
exports.SessionNotCreatedError = SessionNotCreatedError;
exports.StaleElementReferenceError = StaleElementReferenceError;
exports.TimeoutError = TimeoutError;
exports.UnableToSetCookieError = UnableToSetCookieError;
exports.UnableToCaptureScreenError = UnableToCaptureScreenError;
exports.UnexpectedAlertOpenError = UnexpectedAlertOpenError;
exports.UnknownCommandError = UnknownCommandError;
exports.UnknownMethodError = UnknownMethodError;
exports.UnsupportedOperationError = UnsupportedOperationError;

exports.checkResponse = checkResponse;
exports.checkLegacyResponse = checkLegacyResponse;
exports.throwDecodedError = throwDecodedError;
module.exports = {
ErrorCode: ErrorCode,

WebDriverError: WebDriverError,
ElementNotSelectableError: ElementNotSelectableError,
ElementNotVisibleError: ElementNotVisibleError,
InvalidArgumentError: InvalidArgumentError,
InvalidCookieDomainError: InvalidCookieDomainError,
InvalidElementCoordinatesError: InvalidElementCoordinatesError,
InvalidElementStateError: InvalidElementStateError,
InvalidSelectorError: InvalidSelectorError,
InvalidSessionIdError: InvalidSessionIdError,
JavascriptError: JavascriptError,
MoveTargetOutOfBoundsError: MoveTargetOutOfBoundsError,
NoSuchAlertError: NoSuchAlertError,
NoSuchElementError: NoSuchElementError,
NoSuchFrameError: NoSuchFrameError,
NoSuchSessionError: NoSuchSessionError,
NoSuchWindowError: NoSuchWindowError,
ScriptTimeoutError: ScriptTimeoutError,
SessionNotCreatedError: SessionNotCreatedError,
StaleElementReferenceError: StaleElementReferenceError,
TimeoutError: TimeoutError,
UnableToSetCookieError: UnableToSetCookieError,
UnableToCaptureScreenError: UnableToCaptureScreenError,
UnexpectedAlertOpenError: UnexpectedAlertOpenError,
UnknownCommandError: UnknownCommandError,
UnknownMethodError: UnknownMethodError,
UnsupportedOperationError: UnsupportedOperationError,

checkResponse: checkResponse,
checkLegacyResponse: checkLegacyResponse,
encodeError: encodeError,
throwDecodedError: throwDecodedError,
};
6 changes: 4 additions & 2 deletions javascript/node/selenium-webdriver/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,7 @@ class EventEmitter {
// PUBLIC API


exports.EventEmitter = EventEmitter;
exports.Listener = Listener;
module.exports = {
EventEmitter: EventEmitter,
Listener: Listener
};
8 changes: 5 additions & 3 deletions javascript/node/selenium-webdriver/lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class FileDetector {
// PUBLIC API


exports.Button = Button;
exports.Key = Key;
exports.FileDetector = FileDetector;
module.exports = {
Button: Button,
Key: Key,
FileDetector: FileDetector
};
24 changes: 13 additions & 11 deletions javascript/node/selenium-webdriver/lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,16 @@ class Preferences {
// PUBLIC API


exports.Entry = Entry;
exports.Level = Level;
exports.LogManager = LogManager;
exports.Logger = Logger;
exports.Preferences = Preferences;
exports.Type = Type;
exports.addConsoleHandler = addConsoleHandler;
exports.getLevel = getLevel;
exports.getLogger = getLogger;
exports.installConsoleHandler = installConsoleHandler;
exports.removeConsoleHandler = removeConsoleHandler;
module.exports = {
Entry: Entry,
Level: Level,
LogManager: LogManager,
Logger: Logger,
Preferences: Preferences,
Type: Type,
addConsoleHandler: addConsoleHandler,
getLevel: getLevel,
getLogger: getLogger,
installConsoleHandler: installConsoleHandler,
removeConsoleHandler: removeConsoleHandler
};
Loading

0 comments on commit 9bfdbb3

Please sign in to comment.