diff --git a/lib/driverProviders/README.md b/lib/driverProviders/README.md index 2cc0919a1..690b55ecd 100644 --- a/lib/driverProviders/README.md +++ b/lib/driverProviders/README.md @@ -25,12 +25,12 @@ DriverProvider.prototype.getNewDriver /** * @param {webdriver.WebDriver} The driver instance to quit. + * @return {webdriver.promise.Promise} 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} A promise which will resolve when the environment is down. */ DriverProvider.prototype.teardownEnv diff --git a/lib/driverProviders/attachSession.ts b/lib/driverProviders/attachSession.ts index ba3597a9d..dd342a77e 100644 --- a/lib/driverProviders/attachSession.ts +++ b/lib/driverProviders/attachSession.ts @@ -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'; @@ -50,9 +50,7 @@ export class AttachSession extends DriverProvider { * * @public */ - quitDriver(): q.Promise { - let defer = q.defer(); - defer.resolve(null); - return defer.promise; + quitDriver(): wdpromise.Promise { + return wdpromise.when(undefined); } } diff --git a/lib/driverProviders/driverProvider.ts b/lib/driverProviders/driverProvider.ts index fbbaa900a..2a80e383f 100644 --- a/lib/driverProviders/driverProvider.ts +++ b/lib/driverProviders/driverProvider.ts @@ -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'; @@ -65,30 +65,44 @@ export abstract class DriverProvider { * @public * @param webdriver instance */ - quitDriver(driver: WebDriver): q.Promise { + quitDriver(driver: WebDriver): wdpromise.Promise { let driverIndex = this.drivers_.indexOf(driver); if (driverIndex >= 0) { this.drivers_.splice(driverIndex, 1); } - let deferred = q.defer(); if (driver.getSession() === undefined) { - deferred.resolve(); + return wdpromise.when(undefined); } else { - driver.getSession() - .then((session_: Session) => { + return driver.getSession() + .then((session_: Session) => { if (session_) { - driver.quit().then(function() { - deferred.resolve(); - }); - } else { - deferred.resolve(); + return driver.quit(); } }) - .catch((err: Error) => { - deferred.resolve(); - }); + .catch(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 { + let deferred = q.defer(); + wdpromise + .all(drivers.map((driver: WebDriver) => { + return provider.quitDriver(driver); + })) + .then( + () => { + deferred.resolve(); + }, + () => { + deferred.resolve(); + }); return deferred.promise; } @@ -123,12 +137,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} A promise which will resolve when the environment is down. */ - teardownEnv(): q.Promise[]> { - return q.all(this.drivers_.map((driver: WebDriver) => { - return this.quitDriver(driver); - })); + teardownEnv(): q.Promise { + return DriverProvider.quitDrivers(this, this.drivers_); } } diff --git a/lib/runner.ts b/lib/runner.ts index 983e17338..67b01c2c1 100644 --- a/lib/runner.ts +++ b/lib/runner.ts @@ -270,10 +270,9 @@ export class Runner extends EventEmitter { * @return {q.Promise} A promise which resolves on finish. * @private */ - shutdown_(): q.Promise { - return q.all(this.driverprovider_.getExistingDrivers().map((webdriver) => { - return this.driverprovider_.quitDriver(webdriver); - })); + shutdown_(): q.Promise { + return DriverProvider.quitDrivers( + this.driverprovider_, this.driverprovider_.getExistingDrivers()); } /**