Skip to content

Commit c085132

Browse files
committed
Fix converting objects with a toJSON() function to their wire format.
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.
1 parent 7cd16dd commit c085132

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

javascript/node/selenium-webdriver/chrome.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Options.fromCapabilities = function(capabilities) {
257257
addExtensions(o.extensions || []).
258258
detachDriver(!!o.detach).
259259
setChromeBinaryPath(o.binary).
260-
setChromeLogFile(o.logFile).
260+
setChromeLogFile(o.logPath).
261261
setLocalState(o.localState).
262262
setUserPreferences(o.prefs);
263263
}
@@ -417,25 +417,38 @@ Options.prototype.toCapabilities = function(opt_capabilities) {
417417
* detach: boolean,
418418
* extensions: !Array.<string>,
419419
* localState: (Object|undefined),
420-
* logFile: (string|undefined),
420+
* logPath: (string|undefined),
421421
* prefs: (Object|undefined)}} The JSON wire protocol representation
422422
* of this instance.
423423
*/
424424
Options.prototype.toJSON = function() {
425-
return {
425+
var json = {
426426
args: this.args_,
427-
binary: this.binary_,
428427
detach: !!this.detach_,
429428
extensions: this.extensions_.map(function(extension) {
430429
if (Buffer.isBuffer(extension)) {
431430
return extension.toString('base64');
432431
}
433432
return fs.readFileSync(extension, 'base64');
434-
}),
435-
localState: this.localState_,
436-
logFile: this.logFile_,
437-
prefs: this.prefs_
433+
})
438434
};
435+
436+
// ChromeDriver barfs on null keys, so we must ensure these are not included
437+
// if unset (really?)
438+
if (this.binary_) {
439+
json.binary = this.binary_;
440+
}
441+
if (this.localState_) {
442+
json.localState = this.localState_;
443+
}
444+
if (this.logFile_) {
445+
json.logPath = this.logFile_;
446+
}
447+
if (this.prefs_) {
448+
json.prefs = this.prefs_;
449+
}
450+
451+
return json;
439452
};
440453

441454

javascript/node/selenium-webdriver/test/chrome/options_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('chrome.Options', function() {
4848
args: ['a', 'b'],
4949
extensions: [1, 2],
5050
binary: 'binaryPath',
51-
logFile: 'logFilePath',
51+
logPath: 'logFilePath',
5252
detach: true,
5353
localState: 'localStateValue',
5454
prefs: 'prefsValue'
@@ -72,7 +72,7 @@ describe('chrome.Options', function() {
7272
it('should rebuild options from incomplete wire representation',
7373
function() {
7474
var caps = webdriver.Capabilities.chrome().set('chromeOptions', {
75-
logFile: 'logFilePath'
75+
logPath: 'logFilePath'
7676
});
7777

7878
var options = chrome.Options.fromCapabilities(caps);
@@ -83,7 +83,7 @@ describe('chrome.Options', function() {
8383
assert(json.detach).isFalse();
8484
assert(json.extensions.length).equalTo(0);
8585
assert(json.localState).isUndefined();
86-
assert(json.logFile).equalTo('logFilePath');
86+
assert(json.logPath).equalTo('logFilePath');
8787
assert(json.prefs).isUndefined();
8888
});
8989

javascript/webdriver/test/webdriver_test.js

+24
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,30 @@ function testToWireValue_nestedObject() {
442442
}
443443

444444

445+
function testToWireValue_capabilities() {
446+
var prefs = new webdriver.logging.Preferences();
447+
prefs.setLevel(webdriver.logging.Type.BROWSER,
448+
webdriver.logging.Level.DEBUG);
449+
450+
var caps = webdriver.Capabilities.chrome();
451+
caps.set(webdriver.Capability.LOGGING_PREFS, prefs);
452+
453+
var callback = callbackHelper(function(actual) {
454+
webdriver.test.testutil.assertObjectEquals({
455+
'browserName': 'chrome',
456+
'loggingPrefs': {
457+
'browser': 'DEBUG'
458+
}
459+
}, actual);
460+
});
461+
462+
webdriver.WebDriver.toWireValue_(caps).then(callback);
463+
464+
callback.assertCalled();
465+
verifyAll(); // Expected by tear down.
466+
}
467+
468+
445469
function testToWireValue_webElement() {
446470
var expected = {};
447471
expected[webdriver.WebElement.ELEMENT_KEY] = 'fefifofum';

javascript/webdriver/webdriver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ webdriver.WebDriver.toWireValue_ = function(obj) {
191191
return value.getId();
192192
}
193193
if (goog.isFunction(value.toJSON)) {
194-
return value.toJSON();
194+
return convertValue(value.toJSON());
195195
}
196196
if (goog.isNumber(value.nodeType) && goog.isString(value.nodeName)) {
197197
throw new TypeError(

0 commit comments

Comments
 (0)