Skip to content

Commit

Permalink
Fix converting objects with a toJSON() function to their wire format.
Browse files Browse the repository at this point in the history
When we call toJSON() on an object to convert it to it's wire format, we
must recurse on the returned object to make sure all properties are
properly converted.
  • Loading branch information
jleyba committed Oct 30, 2014
1 parent 7cd16dd commit c085132
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
29 changes: 21 additions & 8 deletions javascript/node/selenium-webdriver/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Options.fromCapabilities = function(capabilities) {
addExtensions(o.extensions || []).
detachDriver(!!o.detach).
setChromeBinaryPath(o.binary).
setChromeLogFile(o.logFile).
setChromeLogFile(o.logPath).
setLocalState(o.localState).
setUserPreferences(o.prefs);
}
Expand Down Expand Up @@ -417,25 +417,38 @@ Options.prototype.toCapabilities = function(opt_capabilities) {
* detach: boolean,
* extensions: !Array.<string>,
* localState: (Object|undefined),
* logFile: (string|undefined),
* logPath: (string|undefined),
* prefs: (Object|undefined)}} The JSON wire protocol representation
* of this instance.
*/
Options.prototype.toJSON = function() {
return {
var json = {
args: this.args_,
binary: this.binary_,
detach: !!this.detach_,
extensions: this.extensions_.map(function(extension) {
if (Buffer.isBuffer(extension)) {
return extension.toString('base64');
}
return fs.readFileSync(extension, 'base64');
}),
localState: this.localState_,
logFile: this.logFile_,
prefs: this.prefs_
})
};

// ChromeDriver barfs on null keys, so we must ensure these are not included
// if unset (really?)
if (this.binary_) {
json.binary = this.binary_;
}
if (this.localState_) {
json.localState = this.localState_;
}
if (this.logFile_) {
json.logPath = this.logFile_;
}
if (this.prefs_) {
json.prefs = this.prefs_;
}

return json;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('chrome.Options', function() {
args: ['a', 'b'],
extensions: [1, 2],
binary: 'binaryPath',
logFile: 'logFilePath',
logPath: 'logFilePath',
detach: true,
localState: 'localStateValue',
prefs: 'prefsValue'
Expand All @@ -72,7 +72,7 @@ describe('chrome.Options', function() {
it('should rebuild options from incomplete wire representation',
function() {
var caps = webdriver.Capabilities.chrome().set('chromeOptions', {
logFile: 'logFilePath'
logPath: 'logFilePath'
});

var options = chrome.Options.fromCapabilities(caps);
Expand All @@ -83,7 +83,7 @@ describe('chrome.Options', function() {
assert(json.detach).isFalse();
assert(json.extensions.length).equalTo(0);
assert(json.localState).isUndefined();
assert(json.logFile).equalTo('logFilePath');
assert(json.logPath).equalTo('logFilePath');
assert(json.prefs).isUndefined();
});

Expand Down
24 changes: 24 additions & 0 deletions javascript/webdriver/test/webdriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,30 @@ function testToWireValue_nestedObject() {
}


function testToWireValue_capabilities() {
var prefs = new webdriver.logging.Preferences();
prefs.setLevel(webdriver.logging.Type.BROWSER,
webdriver.logging.Level.DEBUG);

var caps = webdriver.Capabilities.chrome();
caps.set(webdriver.Capability.LOGGING_PREFS, prefs);

var callback = callbackHelper(function(actual) {
webdriver.test.testutil.assertObjectEquals({
'browserName': 'chrome',
'loggingPrefs': {
'browser': 'DEBUG'
}
}, actual);
});

webdriver.WebDriver.toWireValue_(caps).then(callback);

callback.assertCalled();
verifyAll(); // Expected by tear down.
}


function testToWireValue_webElement() {
var expected = {};
expected[webdriver.WebElement.ELEMENT_KEY] = 'fefifofum';
Expand Down
2 changes: 1 addition & 1 deletion javascript/webdriver/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ webdriver.WebDriver.toWireValue_ = function(obj) {
return value.getId();
}
if (goog.isFunction(value.toJSON)) {
return value.toJSON();
return convertValue(value.toJSON());
}
if (goog.isNumber(value.nodeType) && goog.isString(value.nodeName)) {
throw new TypeError(
Expand Down

0 comments on commit c085132

Please sign in to comment.