Skip to content

Commit 9dd3fe4

Browse files
committed
[js] Deprecate more stuff
1 parent c95c848 commit 9dd3fe4

File tree

14 files changed

+150
-66
lines changed

14 files changed

+150
-66
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* Added support for setting the username and password in basic auth pop-up
2525
dialogs (currently IE only).
2626
* Deprecated `WebElement#getInnerHtml()` and `WebEleemnt#getOuterHtml()`
27+
* Deprecated `Promise#thenCatch()` - use `Promise#catch()` instead
28+
* Deprecated `Promise#thenFinally()` - use `promise.thenFinally()` instead
2729
* FIXED: `io.findInPath()` will no longer match against directories that have
2830
the same basename as the target file.
2931
* FIXED: `phantomjs.Driver` now takes a third argument that defines the path to

javascript/node/selenium-webdriver/firefox/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,13 @@ class Driver extends webdriver.WebDriver {
374374
});
375375

376376
onQuit = function() {
377-
return command.then(command => {
377+
let finishCommand = command.then(command => {
378378
command.kill();
379379
return command.result();
380-
}).thenFinally(() => preparedProfile.then(io.rmDir));
380+
});
381+
return promise.thenFinally(
382+
finishCommand,
383+
() => preparedProfile.then(io.rmDir));
381384
};
382385
}
383386

javascript/node/selenium-webdriver/lib/promise.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ class Thenable {
918918
* expect a single argument: the rejection reason.
919919
* @return {!ManagedPromise<R>} A new promise which will be
920920
* resolved wdith the result of the invoked callback.
921+
* @deprecated Use {@link #catch()} instead.
921922
* @template R
922923
*/
923924
thenCatch(errback) {}
@@ -956,6 +957,10 @@ class Thenable {
956957
* to call when this promise is resolved.
957958
* @return {!ManagedPromise<R>} A promise that will be fulfilled
958959
* with the callback result.
960+
* @deprecated thenFinally has been deprecated to help make WebDriver's
961+
* managed promises API compatible with native promises. The functionality
962+
* provided by this method is now offered for any promise implementation
963+
* using the {@link thenFinally} function in the promise module.
959964
* @template R
960965
*/
961966
thenFinally(callback) {}
@@ -1239,12 +1244,21 @@ class ManagedPromise {
12391244
null, errback, 'catch', ManagedPromise.prototype.catch);
12401245
}
12411246

1242-
/** @override */
1247+
/**
1248+
* @override
1249+
* @deprecated Use {@link #catch()} instead.
1250+
*/
12431251
thenCatch(errback) {
12441252
return this.catch(errback);
12451253
}
12461254

1247-
/** @override */
1255+
/**
1256+
* @override
1257+
* @deprecated thenFinally has been deprecated to help make WebDriver's
1258+
* managed promises API compatible with native promises. The functionality
1259+
* provided by this method is now offered for any promise implementation
1260+
* using the {@link thenFinally} function in the promise module.
1261+
*/
12481262
thenFinally(callback) {
12491263
var error;
12501264
var mustThrow = false;
@@ -1438,6 +1452,70 @@ class Deferred {
14381452
Thenable.addImplementation(Deferred);
14391453

14401454

1455+
/**
1456+
* Registers a listener to invoke when a promise is resolved, regardless of
1457+
* whether the promise's value was successfully computed. This function is
1458+
* synonymous with the `finally` clause in a synchronous API:
1459+
*
1460+
* // Synchronous API:
1461+
* try {
1462+
* doSynchronousWork();
1463+
* } finally {
1464+
* cleanUp();
1465+
* }
1466+
*
1467+
* // Asynchronous promise API:
1468+
* promise.finally(doAsynchronousWork(), cleanUp);
1469+
*
1470+
* __Note:__ similar to the `finally` clause, if the registered callback returns
1471+
* a rejected promise or throws an error, that error will silently replace the
1472+
* rejection (if any) from the initial promise:
1473+
*
1474+
* try {
1475+
* throw Error('one');
1476+
* } finally {
1477+
* throw Error('two'); // Hides Error: one
1478+
* }
1479+
*
1480+
* let p = promise.rejected(Error('one'));
1481+
* promise.finally(p, function() {
1482+
* throw Error('two'); // Hides Error: one
1483+
* });
1484+
*
1485+
* @param {PROMISE_TYPE} promise The thenable, promise-like object with which
1486+
* to register the callback.
1487+
* @param {function(): *} callback The function to call when the promise is
1488+
* resolved.
1489+
* @return {!PROMISE_TYPE} A new promise that is chained to the callback. This
1490+
* promise will inherit the value of the original input if the callback
1491+
* does not throw or return a rejected promise.
1492+
* @template PROMISE_TYPE
1493+
*/
1494+
function thenFinally(promise, callback) {
1495+
if (!isPromise(promise)) {
1496+
throw TypeError('first argument must be a promise-like object');
1497+
}
1498+
1499+
if (typeof callback !== 'function') {
1500+
throw TypeError('second argument must be a function');
1501+
}
1502+
1503+
let error;
1504+
let mustThrow = false;
1505+
return promise
1506+
.then(() => callback(), (e) => {
1507+
error = e;
1508+
mustThrow = true;
1509+
return callback();
1510+
})
1511+
.then(() => {
1512+
if (mustThrow) {
1513+
throw error;
1514+
}
1515+
});
1516+
}
1517+
1518+
14411519
/**
14421520
* Tests if a value is an Error-like object. This is more than an straight
14431521
* instanceof check since the value may originate from another context.
@@ -3091,6 +3169,7 @@ module.exports = {
30913169
map: map,
30923170
rejected: rejected,
30933171
setDefaultFlow: setDefaultFlow,
3172+
thenFinally: thenFinally,
30943173
when: when,
30953174

30963175
get LONG_STACK_TRACES() { return LONG_STACK_TRACES; },

javascript/node/selenium-webdriver/lib/until.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ exports.ableToSwitchToFrame = function ableToSwitchToFrame(frame) {
114114
*/
115115
exports.alertIsPresent = function alertIsPresent() {
116116
return new Condition('for alert to be present', function(driver) {
117-
return driver.switchTo().alert().thenCatch(function(e) {
117+
return driver.switchTo().alert().catch(function(e) {
118118
if (!(e instanceof error.NoSuchAlertError)) {
119119
throw e;
120120
}

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,13 @@ class WebDriver {
373373
// actually executes the command. This addresses scenarios like catching
374374
// an element not found error in:
375375
//
376-
// driver.findElement(By.id('foo')).click().thenCatch(function(e) {
376+
// driver.findElement(By.id('foo')).click().catch(function(e) {
377377
// if (e instanceof NoSuchElementError) {
378378
// // Do something.
379379
// }
380380
// });
381381
var prepCommand = toWireValue(command.getParameters());
382-
prepCommand.thenCatch(function() {});
382+
prepCommand.catch(function() {});
383383

384384
var flow = this.flow_;
385385
var executor = this.executor_;
@@ -447,7 +447,7 @@ class WebDriver {
447447
'WebDriver.quit()');
448448
// Delete our session ID when the quit command finishes; this will allow us to
449449
// throw an error when attemnpting to use a driver post-quit.
450-
return result.thenFinally(() => delete this.session_);
450+
return promise.thenFinally(result, () => delete this.session_);
451451
}
452452

453453
/**
@@ -926,7 +926,7 @@ class WebDriver {
926926
setParameter('using', locator.using).
927927
setParameter('value', locator.value);
928928
let res = this.schedule(cmd, 'WebDriver.findElements(' + locator + ')');
929-
return res.thenCatch(function(e) {
929+
return res.catch(function(e) {
930930
if (e instanceof error.NoSuchElementError) {
931931
return [];
932932
}
@@ -1930,7 +1930,7 @@ class WebElement {
19301930
}
19311931

19321932
// Suppress unhandled rejection errors until the flow executes the command.
1933-
keys.thenCatch(function() {});
1933+
keys.catch(function() {});
19341934

19351935
var element = this;
19361936
return this.driver_.flow_.execute(function() {
@@ -2200,10 +2200,10 @@ class WebElementPromise extends WebElement {
22002200
this.catch = el.catch.bind(el);
22012201

22022202
/** @override */
2203-
this.thenCatch = el.thenCatch.bind(el);
2203+
this.thenCatch = el.catch.bind(el);
22042204

22052205
/** @override */
2206-
this.thenFinally = el.thenFinally.bind(el);
2206+
this.thenFinally = (cb) => promise.thenFinally(el, cb);
22072207

22082208
/**
22092209
* Defers returning the element ID until the wrapped WebElement has been
@@ -2354,10 +2354,10 @@ class AlertPromise extends Alert {
23542354
this.catch = alert.catch.bind(alert);
23552355

23562356
/** @override */
2357-
this.thenCatch = alert.thenCatch.bind(alert);
2357+
this.thenCatch = alert.catch.bind(alert);
23582358

23592359
/** @override */
2360-
this.thenFinally = alert.thenFinally.bind(alert);
2360+
this.thenFinally = (cb) => promise.thenFinally(alert, cb);
23612361

23622362
/**
23632363
* Defer returning text until the promised alert has been resolved.

javascript/node/selenium-webdriver/net/portprober.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function findSystemPortRange() {
5353
}
5454
var range = process.platform === 'win32' ?
5555
findWindowsPortRange() : findUnixPortRange();
56-
return systemRange = range.thenCatch(function() {
56+
return systemRange = range.catch(function() {
5757
return DEFAULT_IANA_RANGE;
5858
});
5959
}
@@ -154,7 +154,7 @@ function findWindowsPortRange() {
154154
function isFree(port, opt_host) {
155155
var result = promise.defer();
156156

157-
result.promise.thenCatch(function(e) {
157+
result.promise.catch(function(e) {
158158
if (e instanceof promise.CancellationError) {
159159
server.close();
160160
}

javascript/node/selenium-webdriver/remote/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class DriverService {
237237
return new promise.Promise(function(fulfill, reject) {
238238
var ready = httpUtil.waitForServer(serverUrl, timeout)
239239
.then(fulfill, reject);
240-
earlyTermination.thenCatch(function(e) {
240+
earlyTermination.catch(function(e) {
241241
ready.cancel(/** @type {Error} */(e));
242242
reject(Error(e.message));
243243
});

javascript/node/selenium-webdriver/safari.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class CommandExecutor {
414414
}));
415415
}
416416
var self = this;
417-
return promise.all(tasks).thenFinally(function() {
417+
return promise.thenFinally(promise.all(tasks), function() {
418418
self.server_ = null;
419419
self.socket_ = null;
420420
self.safari_ = null;

javascript/node/selenium-webdriver/test/execute_script_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test.suite(function(env) {
4747
test.it('fails if script throws', function() {
4848
execute('throw new Error("boom")')
4949
.then(function() { throw shoudlHaveFailed; })
50-
.thenCatch(function(e) {
50+
.catch(function(e) {
5151
// The java WebDriver server adds a bunch of crap to error messages.
5252
// Error message will just be "JavaScript error" for IE.
5353
assert(e.message).matches(/.*(JavaScript error|boom).*/);
@@ -57,7 +57,7 @@ test.suite(function(env) {
5757
test.it('fails if script does not parse', function() {
5858
execute('throw function\\*')
5959
.then(function() { throw shoudlHaveFailed; })
60-
.thenCatch(function(e) {
60+
.catch(function(e) {
6161
assert(e).notEqualTo(shouldHaveFailed);
6262
});
6363
});
@@ -323,7 +323,7 @@ test.suite(function(env) {
323323
});
324324
});
325325
});
326-
326+
327327
function verifyJson(expected) {
328328
return function(actual) {
329329
assert(JSON.stringify(actual)).equalTo(JSON.stringify(expected));

javascript/node/selenium-webdriver/test/lib/promise_error_test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,29 @@ describe('promise error handling', function() {
362362
it('start with normal promise', function() {
363363
var error = Error('an error');
364364
return promise.rejected(error).
365-
thenCatch(function(e) {
365+
catch(function(e) {
366366
assert.equal(e, error);
367367
throw new StubError;
368368
}).
369-
thenCatch(assertIsStubError);
369+
catch(assertIsStubError);
370370
});
371371

372372
it('start with task result', function() {
373373
var error = Error('an error');
374374
return flow.execute(function() {
375375
throw error;
376376
}).
377-
thenCatch(function(e) {
377+
catch(function(e) {
378378
assert.equal(e, error);
379379
throw new StubError;
380380
}).
381-
thenCatch(assertIsStubError);
381+
catch(assertIsStubError);
382382
});
383383

384384
it('start with normal promise; uncaught error', function() {
385385
var error = Error('an error');
386386
promise.rejected(error).
387-
thenCatch(function(e) {
387+
catch(function(e) {
388388
assert.equal(e, error);
389389
throw new StubError;
390390
});
@@ -396,7 +396,7 @@ describe('promise error handling', function() {
396396
flow.execute(function() {
397397
throw error;
398398
}).
399-
thenCatch(function(e) {
399+
catch(function(e) {
400400
assert.equal(e, error);
401401
throw new StubError;
402402
});
@@ -429,7 +429,7 @@ describe('promise error handling', function() {
429429

430430
it('promise was rejected', function() {
431431
var toThrow = promise.rejected(new StubError);
432-
toThrow.thenCatch(function() {}); // For tearDown.
432+
toThrow.catch(function() {}); // For tearDown.
433433
flow.execute(function() {
434434
throw toThrow;
435435
}).then(assert.fail, function(e) {
@@ -756,7 +756,7 @@ describe('promise error handling', function() {
756756
}).then(function(error) {
757757
assert.ok(error instanceof promise.CancellationError);
758758
assert.ok(!task.isPending());
759-
return task.thenCatch(function(error) {
759+
return task.catch(function(error) {
760760
assert.ok(error instanceof promise.CancellationError);
761761
});
762762
});

0 commit comments

Comments
 (0)