Skip to content

Commit

Permalink
For consistency with Closure's new promise API, use thenCatch() and t…
Browse files Browse the repository at this point in the history
…henFinally() instead

of addCallback(), addErrback(), et al.
  • Loading branch information
jleyba committed Jan 1, 2014
1 parent 2118dff commit 9c5f645
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 150 deletions.
6 changes: 6 additions & 0 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v2.40.0-dev

* Introduced `Promise#thenCatch()` and `Promise#thenFinally()`.
* Deprecated `Promise#addCallback()`, `Promise#addCallbacks()`,
`Promise#addErrback()`, and `Promise#addBoth()`.

## v2.39.0

* Version bump to stay in sync with the Selenium project.
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/net/portprober.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function findSystemPortRange() {
}
var range = process.platform === 'win32' ?
findWindowsPortRange() : findUnixPortRange();
return systemRange = range.addErrback(function() {
return systemRange = range.thenCatch(function() {
return DEFAULT_IANA_RANGE;
});
}
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function createDriver(opt_capabilities) {
var driver = webdriver.WebDriver.createSession(executor, capabilities);
var boundQuit = driver.quit.bind(driver);
driver.quit = function() {
return boundQuit().addBoth(service.kill.bind(service));
return boundQuit().thenFinally(service.kill.bind(service));
};
return driver;
}
Expand Down
7 changes: 4 additions & 3 deletions javascript/node/selenium-webdriver/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ DriverService.prototype.kill = function() {
this.shutdownHook_ = promise.defer();
this.process_.kill('SIGTERM');
} else {
this.shutdownHook_ = this.address_.addBoth(function() {
this.process_ && this.process_.kill('SIGTERM');
}, this);
var self = this;
this.shutdownHook_ = this.address_.thenFinally(function() {
self.process_ && self.process_.kill('SIGTERM');
});
}
}

Expand Down
12 changes: 6 additions & 6 deletions javascript/node/selenium-webdriver/test/http/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('selenium-webdriver/http/util', function() {
it('should return value field on success', function(done) {
util.getStatus(baseUrl).then(function(response) {
assert.equal('abc123', response);
}).addBoth(done);
}).thenFinally(done);
});

it('should fail if response object is not success', function(done) {
Expand All @@ -76,7 +76,7 @@ describe('selenium-webdriver/http/util', function() {
}, function(err) {
assert.equal(status, err.code);
assert.equal(value, err.message);
}).addBoth(done);
}).thenFinally(done);
});

it('should fail if the server is not listening', function(done) {
Expand All @@ -87,7 +87,7 @@ describe('selenium-webdriver/http/util', function() {
throw Error('expected a failure');
}, function() {
// Expected.
}).addBoth(done);
}).thenFinally(done);
});
});

Expand All @@ -99,7 +99,7 @@ describe('selenium-webdriver/http/util', function() {
}, function(err) {
assert.equal(status, err.code);
assert.equal(value, err.message);
}).addBoth(done);
}).thenFinally(done);
});
});

Expand All @@ -109,7 +109,7 @@ describe('selenium-webdriver/http/util', function() {
setTimeout(function() { status = 0; }, 50);
util.waitForServer(baseUrl, 100).
then(function() {}). // done needs no argument to pass.
addBoth(done);
thenFinally(done);
});

it('should fail if server does not become ready', function(done) {
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('selenium-webdriver/http/util', function() {

util.waitForUrl(baseUrl, 200).
then(function() {}). // done needs no argument to pass.
addBoth(done);
thenFinally(done);
});

it('fails if URL always returns 4xx', function(done) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ test.suite(function(env) {
assert(e.code).equalTo(ErrorCode.SCRIPT_TIMEOUT);
});
}).then(resetPageLoad, function(err) {
resetPageLoad().addBoth(function() {
resetPageLoad().thenFinally(function() {
throw err;
});
});
Expand Down
77 changes: 40 additions & 37 deletions javascript/remote/ui/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ remote.ui.Client.prototype.init = function(opt_element) {
this.scriptButton_.render();
this.sessionContainer_.addControlElement(
/** @type {!Element} */(this.scriptButton_.getElement()));
return this.updateServerInfo_().
addCallback(function() {
this.sessionContainer_.setEnabled(true);
this.onRefresh_();
}, this);
return this.updateServerInfo_().then(goog.bind(function() {
this.sessionContainer_.setEnabled(true);
this.onRefresh_();
}, this));
};


Expand Down Expand Up @@ -199,7 +198,7 @@ remote.ui.Client.prototype.updateServerInfo_ = function() {
this.log_.info('Retrieving server status...');
return this.execute_(
new webdriver.Command(webdriver.CommandName.GET_SERVER_STATUS)).
addCallback(function(response) {
then(goog.bind(function(response) {
var value = response['value'] || {};
var os = value['os'];
if (os && os['name']) {
Expand All @@ -208,7 +207,7 @@ remote.ui.Client.prototype.updateServerInfo_ = function() {
var build = value['build'];
this.serverInfo_.updateInfo(os,
build && build['version'], build && build['revision']);
}, this);
}, this));
};


Expand All @@ -219,17 +218,18 @@ remote.ui.Client.prototype.updateServerInfo_ = function() {
*/
remote.ui.Client.prototype.onRefresh_ = function() {
this.log_.info('Refreshing sessions...');
var self = this;
this.execute_(new webdriver.Command(webdriver.CommandName.GET_SESSIONS)).
addCallback(function(response) {
then(function(response) {
var sessions = response['value'];
sessions = goog.array.map(sessions, function(session) {
return new webdriver.Session(session['id'], session['capabilities']);
});
this.sessionContainer_.refreshSessions(sessions);
}, this).
addErrback(function(e) {
this.logError_('Unable to refresh session list.', e);
}, this);
self.sessionContainer_.refreshSessions(sessions);
}).
thenCatch(function(e) {
self.logError_('Unable to refresh session list.', e);
});
};


Expand All @@ -243,16 +243,17 @@ remote.ui.Client.prototype.onCreate_ = function(e) {
this.log_.info('Creating new session for ' + e.data['browserName']);
var command = new webdriver.Command(webdriver.CommandName.NEW_SESSION).
setParameter('desiredCapabilities', e.data);
var self = this;
this.execute_(command).
addCallback(function(response) {
then(function(response) {
var session = new webdriver.Session(response['sessionId'],
response['value']);
this.sessionContainer_.addSession(session);
}, this).
addErrback(function(e) {
this.logError_('Unable to create new session.', e);
this.sessionContainer_.removePendingTab();
}, this);
self.sessionContainer_.addSession(session);
}).
thenCatch(function(e) {
self.logError_('Unable to create new session.', e);
self.sessionContainer_.removePendingTab();
});
};


Expand All @@ -271,13 +272,15 @@ remote.ui.Client.prototype.onDelete_ = function() {
this.log_.info('Deleting session: ' + session.getId());
var command = new webdriver.Command(webdriver.CommandName.QUIT).
setParameter('sessionId', session.getId());
var self = this;
this.execute_(command).
addCallback(function() {
this.sessionContainer_.removeSession(session);
}, this).
addErrback(function(e) {
this.logError_('Unable to delete session.', e);
}, this);
then(function() {
self.sessionContainer_.removeSession(
/** @type {!webdriver.Session} */(session));
}).
thenCatch(function(e) {
self.logError_('Unable to delete session.', e);
});
};


Expand All @@ -301,10 +304,9 @@ remote.ui.Client.prototype.onLoad_ = function(e) {
setParameter('sessionId', session.getId()).
setParameter('url', url.toString());
this.log_.info('In session(' + session.getId() + '), loading ' + url);
this.execute_(command).
addErrback(function(e) {
this.logError_('Unable to load URL', e);
}, this);
this.execute_(command).thenCatch(goog.bind(function(e) {
this.logError_('Unable to load URL', e);
}, this));
};


Expand All @@ -326,12 +328,13 @@ remote.ui.Client.prototype.onScreenshot_ = function() {
this.screenshotDialog_.setState(remote.ui.ScreenshotDialog.State.LOADING);
this.screenshotDialog_.setVisible(true);

var self = this;
this.execute_(command).
addCallback(function(response) {
this.screenshotDialog_.displayScreenshot(response['value']);
}, this).
addErrback(function(e) {
this.screenshotDialog_.setVisible(false);
this.logError_('Unable to take screenshot.', e);
}, this);
then(function(response) {
self.screenshotDialog_.displayScreenshot(response['value']);
}).
thenCatch(function(e) {
self.screenshotDialog_.setVisible(false);
self.logError_('Unable to take screenshot.', e);
});
};
10 changes: 5 additions & 5 deletions javascript/safari-driver/extension/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ safaridriver.extension.Server.prototype.execute = function(
var flow = webdriver.promise.controlFlow();
var result = flow.execute(fn, description).
then(bot.response.createResponse, bot.response.createErrorResponse).
addBoth(function(response) {
thenFinally(goog.bind(function(response) {
this.session_.setCurrentCommand(null);
return response;
}, this);
}, this));

// If we were given a callback, massage the result to fit the
// webdriver.CommandExecutor contract.
Expand Down Expand Up @@ -434,10 +434,10 @@ safaridriver.extension.Server.prototype.onMessage_ = function(event) {
var command = message.getCommand();

this.execute(command).
addErrback(bot.response.createErrorResponse).
addCallback(function(response) {
thenCatch(bot.response.createErrorResponse).
then(goog.bind(function(response) {
this.send_(command, response);
}, this);
}, this));
};


Expand Down
4 changes: 2 additions & 2 deletions javascript/safari-driver/inject/commandregistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ safaridriver.inject.CommandRegistry.prototype.loadModule_ = function(moduleId) {
}

return safaridriver.inject.util.loadModule(moduleId, this.messageTarget_).
addCallback(function(src) {
then(goog.bind(function(src) {
this.evalModuleFn_(src);
this.loadedModules_[moduleId] = true;
}, this);
}, this));
};
10 changes: 5 additions & 5 deletions javascript/safari-driver/inject/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ safaridriver.inject.Tab.prototype.installPageScript_ = function(opt_dom) {
this.installedPageScript_ = new webdriver.promise.Deferred();

safaridriver.inject.util.loadModule('page_base', safari.self.tab).
addCallback(function(src) {
then(goog.bind(function(src) {
var dom = opt_dom || goog.dom.getDomHelper();
var script = dom.createElement('script');
script.type = 'application/javascript';
Expand All @@ -585,10 +585,10 @@ safaridriver.inject.Tab.prototype.installPageScript_ = function(opt_dom) {
var docEl = dom.getDocument().documentElement;
goog.dom.appendChild(docEl, script);

this.installedPageScript_.addBoth(function() {
this.installedPageScript_.thenFinally(function() {
goog.dom.removeNode(script);
});
}, this);
}, this));
}
return this.installedPageScript_.promise;
};
Expand All @@ -608,7 +608,7 @@ safaridriver.inject.Tab.prototype.executeInPage = function(command) {
bot.response.checkResponse(
/** @type {!bot.response.ResponseObject} */ (decodeResult));

return this.installPageScript_().addCallback(function() {
return this.installPageScript_().then(goog.bind(function() {
var parameters = command.getParameters();
parameters = /** @type {!Object.<*>} */ (this.encoder_.encode(parameters));
command.setParameters(parameters);
Expand All @@ -624,7 +624,7 @@ safaridriver.inject.Tab.prototype.executeInPage = function(command) {
return commandResponse.then(function(result) {
return bot.inject.wrapValue(result);
});
}, this);
}, this));
};


Expand Down
4 changes: 4 additions & 0 deletions javascript/webdriver/exports/exports_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ function checkPromise() {

assertFunction('webdriver.promise', 'Promise');
assertFunction('webdriver.promise.Promise.prototype', 'then');
assertFunction('webdriver.promise.Promise.prototype', 'thenCatch');
assertFunction('webdriver.promise.Promise.prototype', 'thenFinally');
assertFunction('webdriver.promise.Promise.prototype', 'cancel');
assertFunction('webdriver.promise.Promise.prototype', 'addBoth');
assertFunction('webdriver.promise.Promise.prototype', 'addCallback');
Expand All @@ -271,6 +273,8 @@ function checkPromise() {

assertFunction('webdriver.promise', 'Deferred');
assertFunction('new webdriver.promise.Deferred()', 'then');
assertFunction('new webdriver.promise.Deferred()', 'thenCatch');
assertFunction('new webdriver.promise.Deferred()', 'thenFinally');
assertFunction('new webdriver.promise.Deferred()', 'cancel');
assertFunction('new webdriver.promise.Deferred()', 'addBoth');
assertFunction('new webdriver.promise.Deferred()', 'addCallback');
Expand Down
Loading

0 comments on commit 9c5f645

Please sign in to comment.