From 0bdcf15cfd6ccc56056fe696b00126977035d63a Mon Sep 17 00:00:00 2001 From: Sammy Jelin Date: Fri, 3 Feb 2017 16:52:26 -0800 Subject: [PATCH] feat(plugins): allow plugins to know which browser instance to run against Closes https://github.com/angular/protractor/issues/4054 --- lib/browser.ts | 10 +++++----- lib/plugins.ts | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/browser.ts b/lib/browser.ts index c93975834..1a2857d00 100644 --- a/lib/browser.ts +++ b/lib/browser.ts @@ -659,12 +659,12 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver { return this.driver.controlFlow() .execute( () => { - return this.plugins_.waitForPromise(); + return this.plugins_.waitForPromise(this); }, 'Plugins.waitForPromise()') .then(() => { return this.driver.wait(() => { - return this.plugins_.waitForCondition().then((results: boolean[]) => { + return this.plugins_.waitForCondition(this).then((results: boolean[]) => { return results.reduce((x, y) => x && y, true); }); }, this.allScriptsTimeout, 'Plugins.waitForCondition()'); @@ -860,7 +860,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver { url.resolve(this.baseUrl, destination); if (this.ignoreSynchronization) { return this.driver.get(destination) - .then(() => this.driver.controlFlow().execute(() => this.plugins_.onPageLoad())) + .then(() => this.driver.controlFlow().execute(() => this.plugins_.onPageLoad(this))) .then(() => null); } @@ -917,7 +917,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver { .then(() => { // Run Plugins return this.driver.controlFlow().execute(() => { - return this.plugins_.onPageLoad(); + return this.plugins_.onPageLoad(this); }); }) .then(() => { @@ -985,7 +985,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver { .then(() => { // Run Plugins return this.driver.controlFlow().execute(() => { - return this.plugins_.onPageStable(); + return this.plugins_.onPageStable(this); }); }) .then(() => null); diff --git a/lib/plugins.ts b/lib/plugins.ts index 67a5b155a..464f8410f 100644 --- a/lib/plugins.ts +++ b/lib/plugins.ts @@ -1,6 +1,7 @@ import * as q from 'q'; import * as webdriver from 'selenium-webdriver'; +import {ProtractorBrowser} from './browser'; import {Config} from './config'; import {ConfigParser} from './configParser'; import {Logger} from './logger'; @@ -105,6 +106,8 @@ export interface ProtractorPlugin { * This is called inside browser.get() directly after the page loads, and before * angular bootstraps. * + * @param {ProtractorBrowser} browser The browser instance which is loading a page. + * * @this {Object} bound to module.exports * * @throws {*} If this function throws an error, a failed assertion is added to @@ -114,13 +117,15 @@ export interface ProtractorPlugin { * protractor will wait for the promise to resolve before continuing. If * the promise is rejected, a failed assertion is added to the test results. */ - onPageLoad?(): void|webdriver.promise.Promise; + onPageLoad?(browser: ProtractorBrowser): void|webdriver.promise.Promise; /** * This is called inside browser.get() directly after angular is done * bootstrapping/synchronizing. If browser.ignoreSynchronization is true, this * will not be called. * + * @param {ProtractorBrowser} browser The browser instance which is loading a page. + * * @this {Object} bound to module.exports * * @throws {*} If this function throws an error, a failed assertion is added to @@ -130,7 +135,7 @@ export interface ProtractorPlugin { * protractor will wait for the promise to resolve before continuing. If * the promise is rejected, a failed assertion is added to the test results. */ - onPageStable?(): void|webdriver.promise.Promise; + onPageStable?(browser: ProtractorBrowser): void|webdriver.promise.Promise; /** * Between every webdriver action, Protractor calls browser.waitForAngular() to @@ -138,6 +143,8 @@ export interface ProtractorPlugin { * You can use waitForPromise() to have Protractor additionally wait for your * custom promise to be resolved inside of browser.waitForAngular(). * + * @param {ProtractorBrowser} browser The browser instance which needs invoked `waitForAngular`. + * * @this {Object} bound to module.exports * * @throws {*} If this function throws an error, a failed assertion is added to @@ -150,7 +157,7 @@ export interface ProtractorPlugin { * something other than a promise is returned, protractor will continue * onto the next command. */ - waitForPromise?(): webdriver.promise.Promise; + waitForPromise?(browser: ProtractorBrowser): webdriver.promise.Promise; /** * Between every webdriver action, Protractor calls browser.waitForAngular() to @@ -159,6 +166,8 @@ export interface ProtractorPlugin { * custom condition to be truthy. If specified, this function will be called * repeatedly until truthy. * + * @param {ProtractorBrowser} browser The browser instance which needs invoked `waitForAngular`. + * * @this {Object} bound to module.exports * * @throws {*} If this function throws an error, a failed assertion is added to @@ -170,7 +179,7 @@ export interface ProtractorPlugin { * is returned, a failed assertion is added to the test results, and Protractor * will continue onto the next command. */ - waitForCondition?(): webdriver.promise.Promise|boolean; + waitForCondition?(browser: ProtractorBrowser): webdriver.promise.Promise|boolean; /** * Used to turn off default checks for angular stability