Skip to content

Commit

Permalink
[js] Replace the awkward serializable interface with a symbol-based a…
Browse files Browse the repository at this point in the history
…pproach
  • Loading branch information
jleyba committed Feb 9, 2016
1 parent 4ddce09 commit c5b6f23
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 144 deletions.
7 changes: 2 additions & 5 deletions javascript/node/selenium-webdriver/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const executors = require('./executors'),
Capabilities = require('./lib/capabilities').Capabilities,
Capability = require('./lib/capabilities').Capability,
promise = require('./lib/promise'),
serializable = require('./lib/serializable'),
Symbols = require('./lib/symbols'),
webdriver = require('./lib/webdriver'),
portprober = require('./net/portprober'),
remote = require('./remote');
Expand Down Expand Up @@ -369,7 +369,6 @@ let OPTIONS_CAPABILITY_KEY = 'chromeOptions';

/**
* Class for managing ChromeDriver specific options.
* @implements serializable.Serializable
*/
class Options {
constructor() {
Expand Down Expand Up @@ -735,9 +734,8 @@ class Options {
* logPath: (string|undefined),
* prefs: (Object|undefined)}} The JSON wire protocol representation
* of this instance.
* @override
*/
serialize() {
[Symbols.serialize]() {
let json = {};
for (let key in this.options_) {
if (this.options_[key] != null) {
Expand All @@ -756,7 +754,6 @@ class Options {
return json;
}
}
serializable.setSerializable(Options);


/**
Expand Down
7 changes: 2 additions & 5 deletions javascript/node/selenium-webdriver/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const fs = require('fs'),
const executors = require('./executors'),
io = require('./io'),
capabilities = require('./lib/capabilities'),
serializable = require('./lib/serializable'),
Symbols = require('./lib/symbols'),
webdriver = require('./lib/webdriver'),
portprober = require('./net/portprober'),
remote = require('./remote');
Expand All @@ -95,7 +95,6 @@ const CAPABILITY_KEY = {

/**
* Class for managing MicrosoftEdgeDriver specific options.
* @implements {serializable.Serializable}
*/
class Options {
constructor() {
Expand Down Expand Up @@ -176,9 +175,8 @@ class Options {
* function is an implementation not intended for general use.
* @return {{pageLoadStrategy: (string|undefined)}
* The JSON wire protocol representation of this instance.
* @override
*/
serialize() {
[Symbols.serialize]() {
var json = {};
for (var key in this.options_) {
if (this.options_[key] != null) {
Expand All @@ -188,7 +186,6 @@ class Options {
return json;
}
}
serializable.setSerializable(Options);


/**
Expand Down
7 changes: 2 additions & 5 deletions javascript/node/selenium-webdriver/firefox/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const child = require('child_process'),
util = require('util');

const isDevMode = require('../lib/devmode'),
serializable = require('../lib/serializable'),
promise = require('../lib/promise'),
Symbols = require('../lib/symbols'),
io = require('../io'),
exec = require('../io/exec');

Expand Down Expand Up @@ -136,7 +136,6 @@ function installNoFocusLibs(profileDir) {
* Provides a mechanism to configure and launch Firefox in a subprocess for
* use with WebDriver.
*
* @implements {serializable.Serializable<string>}
* @final
*/
class Binary {
Expand Down Expand Up @@ -208,13 +207,11 @@ class Binary {
*
* @return {!promise.Promise<string>} A promise for this binary's wire
* representation.
* @override
*/
serialize() {
[Symbols.serialize]() {
return promise.fulfilled(this.exe_ || findFirefox());
}
}
serializable.setSerializable(Binary);


// PUBLIC API
Expand Down
7 changes: 2 additions & 5 deletions javascript/node/selenium-webdriver/firefox/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const AdmZip = require('adm-zip'),
vm = require('vm');

const isDevMode = require('../lib/devmode'),
serializable = require('../lib/serializable'),
promise = require('../lib/promise'),
Symbols = require('../lib/symbols'),
io = require('../io'),
extension = require('./extension');

Expand Down Expand Up @@ -207,7 +207,6 @@ function decode(data) {
* Models a Firefox proifle directory for use with the FirefoxDriver. The
* {@code Proifle} directory uses an in-memory model until {@link #writeToDisk}
* is called.
* @implements {serializable.Serializable<string>}
*/
class Profile {
/**
Expand Down Expand Up @@ -391,13 +390,11 @@ class Profile {
/**
* Encodes this profile as a zipped, base64 encoded directory.
* @return {!promise.Promise<string>} A promise for the encoded profile.
* @override
*/
serialize() {
[Symbols.serialize]() {
return this.encode();
}
}
serializable.setSerializable(Profile);


// PUBLIC API
Expand Down
2 changes: 0 additions & 2 deletions javascript/node/selenium-webdriver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const events = require('./lib/events');
const input = require('./lib/input');
const logging = require('./lib/logging');
const promise = require('./lib/promise');
const serializable = require('./lib/serializable');
const session = require('./lib/session');
const until = require('./lib/until');
const webdriver = require('./lib/webdriver');
Expand All @@ -48,7 +47,6 @@ exports.Capability = capabilities.Capability;
exports.EventEmitter = events.EventEmitter;
exports.FileDetector = input.FileDetector;
exports.Key = input.Key;
exports.Serializable = serializable.Serializable;
exports.Session = session.Session;
exports.WebDriver = webdriver.WebDriver;
exports.WebElement = webdriver.WebElement;
Expand Down
8 changes: 2 additions & 6 deletions javascript/node/selenium-webdriver/lib/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* WebDriver session.
*/

const serializable = require('./serializable');
const Symbols = require('./symbols');


/**
Expand Down Expand Up @@ -172,8 +172,6 @@ function toMap(hash) {

/**
* Describes a set of capabilities for a WebDriver session.
*
* @implements {serializable.Serializable<!Object<string, ?>>}
*/
class Capabilities extends Map {
/**
Expand Down Expand Up @@ -283,9 +281,8 @@ class Capabilities extends Map {
/**
* @return {!Object<string, ?>} The JSON representation of this instance.
* Note, the returned object may contain nested promised values.
* @override
*/
serialize() {
[Symbols.serialize]() {
let ret = {};
for (let key of this.keys()) {
let cap = this.get(key);
Expand Down Expand Up @@ -384,7 +381,6 @@ class Capabilities extends Map {
return this.set(Capability.UNEXPECTED_ALERT_BEHAVIOR, behavior);
}
}
serializable.setSerializable(Capabilities);


// PUBLIC API
Expand Down
87 changes: 0 additions & 87 deletions javascript/node/selenium-webdriver/lib/serializable.js

This file was deleted.

36 changes: 36 additions & 0 deletions javascript/node/selenium-webdriver/lib/symbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

'use strict';

/**
* @fileoverview Defines well-known symbols used within the selenium-webdriver
* library.
*/


/**
* The serialize symbol specifies a method that returns an object's serialized
* representation. If an object's serialized form is not immediately available,
* the serialize method will return a promise that will be resolved with the
* serialized form.
*
* Note that the described method is analgous to objects that define a
* `toJSON()` method, except the serialized result may be a promise, or another
* object with a promised property.
*/
exports.serialize = Symbol('serialize');
20 changes: 9 additions & 11 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const Capabilities = require('./capabilities').Capabilities;
const command = require('./command');
const input = require('./input');
const logging = require('./logging');
const serializable = require('./serializable');
const Session = require('./session').Session;
const Symbols = require('./symbols');
const promise = require('./promise');
const until = require('./until');

Expand Down Expand Up @@ -92,9 +92,8 @@ function executeCommand(executor, command) {
* <ol>
* <li>if the object is a WebElement, the return value will be the element's
* server ID
* <li>if the object is a Serializable, its
* {@link serializable.Serializable#serialize} function will be invoked and
* this algorithm will recursively be applied to the result
* <li>if the object defines a {@link Symbols.serialize} method, this algorithm
* will be recursively applied to the object's serialized representation
* <li>if the object provides a "toJSON" function, this algorithm will
* recursively be applied to the result of that function
* <li>otherwise, the value of each key will be recursively converted according
Expand Down Expand Up @@ -132,8 +131,8 @@ function convertValue(value) {
return '' + value;
}

if (serializable.isSerializable(value)) {
return toWireValue(value.serialize());
if (typeof value[Symbols.serialize] === 'function') {
return toWireValue(value[Symbols.serialize]());
} else if (typeof value.toJSON === 'function') {
return toWireValue(value.toJSON());
}
Expand Down Expand Up @@ -1523,8 +1522,6 @@ const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf';
* var searchForm = driver.findElement(By.tagName('form'));
* var searchBox = searchForm.findElement(By.name('q'));
* searchBox.sendKeys('webdriver');
*
* @implements {serializable.Serializable}
*/
class WebElement {
/**
Expand Down Expand Up @@ -1624,8 +1621,10 @@ class WebElement {
return this.getId();
}

/** @override */
serialize() {
/**
* @return {!Object} Returns the serialized representation of this WebElement.
*/
[Symbols.serialize]() {
return this.getId().then(WebElement.buildId);
}

Expand Down Expand Up @@ -2044,7 +2043,6 @@ class WebElement {
return this.driver_.executeScript('return arguments[0].innerHTML', this);
}
}
serializable.setSerializable(WebElement);


/**
Expand Down
Loading

0 comments on commit c5b6f23

Please sign in to comment.