Skip to content

Commit

Permalink
chore(quitDriver): have quitDriver return a webdriver promise directly (
Browse files Browse the repository at this point in the history
angular#3992)

Wrapping it in a `q` promise is blocking angular#3899

Closes angular#3902

Custom frameworks might not make this change but it'll be fine.  It'll only be a
problem in edge cases and they probably weren't returning the right promise
before anyway.
  • Loading branch information
sjelin committed Jan 27, 2017
1 parent 6c3be8c commit 46c8898
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/driverProviders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ DriverProvider.prototype.getNewDriver

/**
* @param {webdriver.WebDriver} The driver instance to quit.
* @return {webdriver.promise.Promise<void>} A promise which resolves when the instance has quit
*/
DriverProvider.prototype.quitDriver

/**
* @return {q.promise} A promise which will resolve when the environment
* is down.
* @return {q.Promise<any>} A promise which will resolve when the environment is down.
*/
DriverProvider.prototype.teardownEnv

Expand Down
8 changes: 3 additions & 5 deletions lib/driverProviders/attachSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* it down, and setting up the driver correctly.
*/
import * as q from 'q';
import {WebDriver} from 'selenium-webdriver';
import {promise as wdpromise, WebDriver} from 'selenium-webdriver';

import {Config} from '../config';
import {Logger} from '../logger';
Expand Down Expand Up @@ -50,9 +50,7 @@ export class AttachSession extends DriverProvider {
*
* @public
*/
quitDriver(): q.Promise<WebDriver> {
let defer = q.defer<WebDriver>();
defer.resolve(null);
return defer.promise;
quitDriver(): wdpromise.Promise<void> {
return wdpromise.when(undefined);
}
}
51 changes: 31 additions & 20 deletions lib/driverProviders/driverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* it down, and setting up the driver correctly.
*/
import * as q from 'q';
import {Builder, Session, WebDriver} from 'selenium-webdriver';
import {Builder, promise as wdpromise, Session, WebDriver} from 'selenium-webdriver';

import {BlockingProxyRunner} from '../bpRunner';
import {Config} from '../config';
Expand Down Expand Up @@ -68,30 +68,44 @@ export abstract class DriverProvider {
* @public
* @param webdriver instance
*/
quitDriver(driver: WebDriver): q.Promise<WebDriver> {
quitDriver(driver: WebDriver): wdpromise.Promise<void> {
let driverIndex = this.drivers_.indexOf(driver);
if (driverIndex >= 0) {
this.drivers_.splice(driverIndex, 1);
}

let deferred = q.defer<WebDriver>();
if (driver.getSession() === undefined) {
deferred.resolve();
return wdpromise.when(undefined);
} else {
driver.getSession()
.then((session_: Session) => {
return driver.getSession()
.then<void>((session_: Session) => {
if (session_) {
driver.quit().then(function() {
deferred.resolve();
});
} else {
deferred.resolve();
return driver.quit();
}
})
.catch((err: Error) => {
deferred.resolve();
});
.catch<void>(function(err: Error) {});
}
}


/**
* Quits an array of drivers and returns a q promise instead of a webdriver one
*
* @param drivers {webdriver.WebDriver[]} The webdriver instances
*/
static quitDrivers(provider: DriverProvider, drivers: WebDriver[]): q.Promise<void> {
let deferred = q.defer<void>();
wdpromise
.all(drivers.map((driver: WebDriver) => {
return provider.quitDriver(driver);
}))
.then(
() => {
deferred.resolve();
},
() => {
deferred.resolve();
});
return deferred.promise;
}

Expand Down Expand Up @@ -126,12 +140,9 @@ export abstract class DriverProvider {
* Shuts down the drivers.
*
* @public
* @return {q.promise} A promise which will resolve when the environment
* is down.
* @return {q.Promise<any>} A promise which will resolve when the environment is down.
*/
teardownEnv(): q.Promise<q.Promise<WebDriver>[]> {
return q.all<any>(this.drivers_.map((driver: WebDriver) => {
return this.quitDriver(driver);
}));
teardownEnv(): q.Promise<any> {
return DriverProvider.quitDrivers(this, this.drivers_);
}
}
7 changes: 3 additions & 4 deletions lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,9 @@ export class Runner extends EventEmitter {
* @return {q.Promise} A promise which resolves on finish.
* @private
*/
shutdown_(): q.Promise<any> {
return q.all(this.driverprovider_.getExistingDrivers().map((webdriver) => {
return this.driverprovider_.quitDriver(webdriver);
}));
shutdown_(): q.Promise<void> {
return DriverProvider.quitDrivers(
this.driverprovider_, this.driverprovider_.getExistingDrivers());
}

/**
Expand Down

0 comments on commit 46c8898

Please sign in to comment.