From 8f8321804ccdc2c7601ff67ca50f719db77e0ff8 Mon Sep 17 00:00:00 2001 From: Sammy Jelin Date: Thu, 29 Dec 2016 21:25:52 -0800 Subject: [PATCH] chore(frameworks): allow frameworks to call `afterEach`. Follow up to https://github.com/angular/protractor/pull/3893. Part of #3893 --- lib/runner.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/runner.ts b/lib/runner.ts index c720e0f69..f05e4a6e0 100644 --- a/lib/runner.ts +++ b/lib/runner.ts @@ -33,6 +33,8 @@ export class Runner extends EventEmitter { driverprovider_: DriverProvider; o: any; plugins_: Plugins; + restartPromise: q.Promise; + afterEachWasCalled: boolean; constructor(config: Config) { super(); @@ -86,6 +88,26 @@ export class Runner extends EventEmitter { }); } + /** + * Called after each test finishes. + * + * Responsible for `restartBrowserBetweenTests` + * + * @public + * @return {q.Promise} A promise that will resolve when the work here is done + */ + afterEach(): q.Promise { + let ret: q.Promise; + this.afterEachWasCalled = true; + if (this.config_.restartBrowserBetweenTests) { + // TODO(sjelin): remove the `|| q()` once `restart()` returns a promise + this.restartPromise = this.restartPromise || protractor.browser.restart() || q(); + ret = this.restartPromise; + this.restartPromise = undefined; + } + return ret || q(); + } + /** * Grab driver provider based on type * @private @@ -325,8 +347,12 @@ export class Runner extends EventEmitter { } if (this.config_.restartBrowserBetweenTests) { + // TODO(sjelin): replace with warnings once `afterEach` support is required let restartDriver = () => { - browser_.restart(); + if (!this.afterEachWasCalled) { + // TODO(sjelin): remove the `|| q()` once `restart()` returns a promise + this.restartPromise = browser_.restart() || q(); + } }; this.on('testPass', restartDriver); this.on('testFail', restartDriver);