From 66a86fd64fd9f5d3551b1c60d8a0ca8f9d871dae Mon Sep 17 00:00:00 2001 From: Craig Nishina Date: Tue, 22 Nov 2016 15:25:10 -0800 Subject: [PATCH] fix(ExpectedConditions): non-static ExpectedConditions for browser - update sauce lab binary to run on travis closes #3761 --- lib/browser.ts | 5 +- lib/expectedConditions.ts | 63 +++++++++++--------------- lib/runner.ts | 2 +- scripts/sauce_connect_setup.sh | 6 +-- spec/basic/expected_conditions_spec.js | 17 +++++++ spec/basic/handling_spec.js | 2 +- spec/dependencyTest/protractor_spec.js | 2 +- 7 files changed, 54 insertions(+), 43 deletions(-) diff --git a/lib/browser.ts b/lib/browser.ts index 2d23dc314..07626413d 100644 --- a/lib/browser.ts +++ b/lib/browser.ts @@ -131,7 +131,7 @@ export class ProtractorBrowser extends Webdriver { /** * @type {ExpectedConditions} */ - static ExpectedConditions = new ProtractorExpectedConditions(); + ExpectedConditions: ProtractorExpectedConditions; /** * The wrapped webdriver instance. Use this to interact with pages that do @@ -320,6 +320,9 @@ export class ProtractorBrowser extends Webdriver { this.trackOutstandingTimeouts_ = !opt_untrackOutstandingTimeouts; this.mockModules_ = []; this.addBaseMockModules_(); + + // set up expected conditions + this.ExpectedConditions = new ProtractorExpectedConditions(this); } /** diff --git a/lib/expectedConditions.ts b/lib/expectedConditions.ts index 694acd541..740e9770e 100644 --- a/lib/expectedConditions.ts +++ b/lib/expectedConditions.ts @@ -1,5 +1,5 @@ +import {ProtractorBrowser} from './browser'; import {ElementFinder} from './element'; -import {Ptor} from './ptor'; let webdriver = require('selenium-webdriver'); @@ -47,6 +47,8 @@ declare var global: any; * @constructor */ export class ProtractorExpectedConditions { + constructor(private browser: ProtractorBrowser){}; + /** * Negates the result of a promise. * @@ -155,21 +157,18 @@ export class ProtractorExpectedConditions { */ alertIsPresent(): Function { return () => { - return (global.protractor) - .browser.driver.switchTo() - .alert() - .then( - (): - boolean => { - return true; - }, - (err: any) => { - if (err.code == webdriver.error.ErrorCode.NO_SUCH_ALERT) { - return false; - } else { - throw err; - } - }); + return this.browser.driver.switchTo().alert().then( + (): + boolean => { + return true; + }, + (err: any) => { + if (err.code == webdriver.error.ErrorCode.NO_SUCH_ALERT) { + return false; + } else { + throw err; + } + }); }; } @@ -261,11 +260,9 @@ export class ProtractorExpectedConditions { */ titleContains(title: string): Function { return () => { - return (global.protractor) - .browser.driver.getTitle() - .then((actualTitle: string): boolean => { - return actualTitle.indexOf(title) > -1; - }); + return this.browser.driver.getTitle().then((actualTitle: string): boolean => { + return actualTitle.indexOf(title) > -1; + }); }; } @@ -285,11 +282,9 @@ export class ProtractorExpectedConditions { */ titleIs(title: string): Function { return () => { - return (global.protractor) - .browser.driver.getTitle() - .then((actualTitle: string): boolean => { - return actualTitle === title; - }); + return this.browser.driver.getTitle().then((actualTitle: string): boolean => { + return actualTitle === title; + }); }; } @@ -310,11 +305,9 @@ export class ProtractorExpectedConditions { */ urlContains(url: string): Function { return () => { - return (global.protractor) - .browser.driver.getCurrentUrl() - .then((actualUrl: string): boolean => { - return actualUrl.indexOf(url) > -1; - }); + return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => { + return actualUrl.indexOf(url) > -1; + }); }; } @@ -334,11 +327,9 @@ export class ProtractorExpectedConditions { */ urlIs(url: string): Function { return () => { - return (global.protractor) - .browser.driver.getCurrentUrl() - .then((actualUrl: string): boolean => { - return actualUrl === url; - }); + return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => { + return actualUrl === url; + }); }; } diff --git a/lib/runner.ts b/lib/runner.ts index eee47b4c2..7ad8d2eac 100644 --- a/lib/runner.ts +++ b/lib/runner.ts @@ -167,7 +167,7 @@ export class Runner extends EventEmitter { protractor.element = browser_.element; protractor.by = protractor.By = ProtractorBrowser.By; protractor.wrapDriver = ProtractorBrowser.wrapDriver; - protractor.ExpectedConditions = ProtractorBrowser.ExpectedConditions; + protractor.ExpectedConditions = browser_.ExpectedConditions; if (!this.config_.noGlobals) { // Export protractor to the global namespace to be used in tests. diff --git a/scripts/sauce_connect_setup.sh b/scripts/sauce_connect_setup.sh index f0bc12aaf..b259bc469 100755 --- a/scripts/sauce_connect_setup.sh +++ b/scripts/sauce_connect_setup.sh @@ -12,9 +12,9 @@ set -e # before_script: # - curl https://gist.github.com/santiycr/5139565/raw/sauce_connect_setup.sh | bash -CONNECT_URL="https://saucelabs.com/downloads/sc-4.4.0-linux.tar.gz" +CONNECT_URL="https://saucelabs.com/downloads/sc-4.4.1-linux.tar.gz" CONNECT_DIR="/tmp/sauce-connect-$RANDOM" -CONNECT_DOWNLOAD="sc-4.4.0-linux.tar.gz" +CONNECT_DOWNLOAD="sc-4.4.1-linux.tar.gz" CONNECT_LOG="$LOGS_DIR/sauce-connect" CONNECT_STDOUT="$LOGS_DIR/sauce-connect.stdout" @@ -52,4 +52,4 @@ sauce-connect/bin/sc -u $SAUCE_USERNAME -k $SAUCE_ACCESS_KEY $ARGS \ # If you need to debug sauce connect, use the full output like so: # # sauce-connect/bin/sc -u $SAUCE_USERNAME -k $SAUCE_ACCESS_KEY $ARGS \ -# --logfile $CONNECT_LOG 2> $CONNECT_STDERR 1> $CONNECT_STDOUT & \ No newline at end of file +# --logfile $CONNECT_LOG 2> $CONNECT_STDERR 1> $CONNECT_STDOUT & diff --git a/spec/basic/expected_conditions_spec.js b/spec/basic/expected_conditions_spec.js index 705e08d52..916aa66b2 100644 --- a/spec/basic/expected_conditions_spec.js +++ b/spec/basic/expected_conditions_spec.js @@ -176,4 +176,21 @@ describe('expected conditions', function() { expect(EC.or(valid, EC.and(valid, invalid)).call()).toBe(true); expect(EC.or(EC.not(valid), EC.and(valid, invalid)).call()).toBe(false); }); + + describe('for forked browsers', function() { + // ensure that we can run EC on forked browser instances + it('should have alertIsPresent', function() { + var browser2 = browser.forkNewDriverInstance(); + browser2.get('index.html#/form'); + var EC2 = browser2.ExpectedConditions; + var alertIsPresent = EC2.alertIsPresent(); + expect(alertIsPresent.call()).toBe(false); + + var alertButton = browser2.$('#alertbutton'); + alertButton.click(); + browser2.wait(EC2.alertIsPresent(), 1000); + + browser2.switchTo().alert().accept(); + }); + }); }); diff --git a/spec/basic/handling_spec.js b/spec/basic/handling_spec.js index da3cd5337..60c359138 100644 --- a/spec/basic/handling_spec.js +++ b/spec/basic/handling_spec.js @@ -5,7 +5,7 @@ describe('handling timeout errors', function() { browser.get('http://dummyUrl', 1).then(function() { throw 'did not handle error'; }, function(err) { - expect(err).toBeDefined(); + expect(err instanceof Error).toBeTruthy(); }); }); }); diff --git a/spec/dependencyTest/protractor_spec.js b/spec/dependencyTest/protractor_spec.js index dec44bdcb..202f1e4c6 100644 --- a/spec/dependencyTest/protractor_spec.js +++ b/spec/dependencyTest/protractor_spec.js @@ -42,7 +42,7 @@ describe('require(\'protractor\')', () => { }); it('should have static variables defined', () => { - var staticVariables = ['By', 'ExpectedConditions']; + var staticVariables = ['By']; for (var pos in staticVariables) { var property = staticVariables[pos]; expect(typeof protractor.ProtractorBrowser[property]).toEqual('object');