From cee662a33213add2fa3ba375c3d75c35212161b1 Mon Sep 17 00:00:00 2001 From: Craig Nishina Date: Thu, 20 Dec 2018 10:34:56 -0800 Subject: [PATCH] chore(expectedConditions): update generic Function typings - Use `() => Promise` over `Function` typings. - Fix an ExpectedConditions test where it was set to a const. - Fix a TypeScript typing interface issue with RunResults in taskRunner. --- lib/expectedConditions.ts | 129 ++++++++++++------------- lib/taskRunner.ts | 12 +-- spec/basic/expected_conditions_spec.js | 5 +- 3 files changed, 73 insertions(+), 73 deletions(-) diff --git a/lib/expectedConditions.ts b/lib/expectedConditions.ts index 30850b0c6..139dcfbd9 100644 --- a/lib/expectedConditions.ts +++ b/lib/expectedConditions.ts @@ -59,11 +59,10 @@ export class ProtractorExpectedConditions { * * @returns {!function} An expected condition that returns the negated value. */ - not(expectedCondition: Function): Function { - return (): Function => { - return expectedCondition().then((bool: boolean): boolean => { - return !bool; - }); + not(expectedCondition: Function): (() => Promise) { + return async(): Promise => { + const bool = await expectedCondition(); + return !bool; }; } @@ -78,20 +77,19 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise which * evaluates to the result of the logical chain. */ - logicalChain_(defaultRet: boolean, fns: Array): Function { + logicalChain_(defaultRet: boolean, fns: Array): (() => Promise) { let self = this; - return (): boolean => { + return async(): Promise => { if (fns.length === 0) { return defaultRet; } - let fn = fns[0]; - return fn().then((bool: boolean): boolean => { - if (bool === defaultRet) { - return self.logicalChain_(defaultRet, fns.slice(1))(); - } else { - return !defaultRet; - } - }); + const fn = fns[0]; + const bool = await fn(); + if (bool === defaultRet) { + return self.logicalChain_(defaultRet, fns.slice(1))(); + } else { + return !defaultRet; + } }; } @@ -113,7 +111,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise which * evaluates to the result of the logical and. */ - and(...args: Function[]): Function { + and(...args: Function[]): (() => Promise) { return this.logicalChain_(true, args); } @@ -135,7 +133,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise which * evaluates to the result of the logical or. */ - or(...args: Function[]): Function { + or(...args: Function[]): (() => Promise) { return this.logicalChain_(false, args); } @@ -151,20 +149,18 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether an alert is present. */ - alertIsPresent(): Function { - return () => { - return this.browser.driver.switchTo().alert().then( - (): - boolean => { - return true; - }, - (err: any) => { - if (err instanceof wderror.NoSuchAlertError) { - return false; - } else { - throw err; - } - }); + alertIsPresent(): (() => Promise) { + return async(): Promise => { + try { + await this.browser.driver.switchTo().alert(); + return true; + } catch (e) { + if (e instanceof wderror.NoSuchAlertError) { + return false; + } else { + throw e; + } + } }; } @@ -183,7 +179,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is clickable. */ - elementToBeClickable(elementFinder: ElementFinder): Function { + elementToBeClickable(elementFinder: ElementFinder): (() => Promise) { return this.and(this.visibilityOf(elementFinder), () => { return elementFinder.isEnabled().then(passBoolean, falseIfMissing); }); @@ -205,13 +201,16 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the text is present in the element. */ - textToBePresentInElement(elementFinder: ElementFinder, text: string): Function { - let hasText = () => { - return elementFinder.getText().then((actualText: string): boolean => { + textToBePresentInElement(elementFinder: ElementFinder, text: string): (() => Promise) { + let hasText = async () => { + try { + const actualText = await elementFinder.getText(); // MSEdge does not properly remove newlines, which causes false // negatives return actualText.replace(/\r?\n|\r/g, '').indexOf(text) > -1; - }, falseIfMissing); + } catch (e) { + return falseIfMissing(e); + } }; return this.and(this.presenceOf(elementFinder), hasText); } @@ -232,11 +231,15 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the text is present in the element's value. */ - textToBePresentInElementValue(elementFinder: ElementFinder, text: string): Function { - let hasText = () => { - return elementFinder.getAttribute('value').then((actualText: string): boolean => { + textToBePresentInElementValue(elementFinder: ElementFinder, text: string): + (() => Promise) { + let hasText = async () => { + try { + const actualText = await elementFinder.getAttribute('value'); return actualText.indexOf(text) > -1; - }, falseIfMissing); + } catch (e) { + return falseIfMissing(e); + } }; return this.and(this.presenceOf(elementFinder), hasText); } @@ -256,11 +259,10 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the title contains the string. */ - titleContains(title: string): Function { - return () => { - return this.browser.driver.getTitle().then((actualTitle: string): boolean => { - return actualTitle.indexOf(title) > -1; - }); + titleContains(title: string): (() => Promise) { + return async(): Promise => { + const actualTitle = await this.browser.driver.getTitle(); + return actualTitle.indexOf(title) > -1; }; } @@ -278,11 +280,10 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the title equals the string. */ - titleIs(title: string): Function { - return () => { - return this.browser.driver.getTitle().then((actualTitle: string): boolean => { - return actualTitle === title; - }); + titleIs(title: string): (() => Promise) { + return async(): Promise => { + const actualTitle = await this.browser.driver.getTitle(); + return actualTitle === title; }; } @@ -301,11 +302,10 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the URL contains the string. */ - urlContains(url: string): Function { - return () => { - return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => { - return actualUrl.indexOf(url) > -1; - }); + urlContains(url: string): (() => Promise) { + return async(): Promise => { + const actualUrl = await this.browser.driver.getCurrentUrl(); + return actualUrl.indexOf(url) > -1; }; } @@ -323,11 +323,10 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the url equals the string. */ - urlIs(url: string): Function { - return () => { - return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => { - return actualUrl === url; - }); + urlIs(url: string): (() => Promise) { + return async(): Promise => { + const actualUrl = await this.browser.driver.getCurrentUrl(); + return actualUrl === url; }; } @@ -347,7 +346,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is present. */ - presenceOf(elementFinder: ElementFinder): Function { + presenceOf(elementFinder: ElementFinder): (() => Promise) { return elementFinder.isPresent.bind(elementFinder); } @@ -366,7 +365,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is stale. */ - stalenessOf(elementFinder: ElementFinder): Function { + stalenessOf(elementFinder: ElementFinder): (() => Promise) { return this.not(this.presenceOf(elementFinder)); } @@ -388,7 +387,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is visible. */ - visibilityOf(elementFinder: ElementFinder): Function { + visibilityOf(elementFinder: ElementFinder): (() => Promise) { return this.and(this.presenceOf(elementFinder), () => { return elementFinder.isDisplayed().then(passBoolean, falseIfMissing); }); @@ -409,7 +408,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is invisible. */ - invisibilityOf(elementFinder: ElementFinder): Function { + invisibilityOf(elementFinder: ElementFinder): (() => Promise) { return this.not(this.visibilityOf(elementFinder)); } @@ -427,7 +426,7 @@ export class ProtractorExpectedConditions { * @returns {!function} An expected condition that returns a promise * representing whether the element is selected. */ - elementToBeSelected(elementFinder: ElementFinder): Function { + elementToBeSelected(elementFinder: ElementFinder): (() => Promise) { return this.and(this.presenceOf(elementFinder), () => { return elementFinder.isSelected().then(passBoolean, falseIfMissing); }); diff --git a/lib/taskRunner.ts b/lib/taskRunner.ts index c1402411c..3c5579b29 100644 --- a/lib/taskRunner.ts +++ b/lib/taskRunner.ts @@ -7,12 +7,12 @@ import {Runner} from './runner'; import {TaskLogger} from './taskLogger'; export interface RunResults { - taskId: number; - specs: Array; - capabilities: any; - failedCount: number; - exitCode: number; - specResults: Array; + taskId?: number; + specs?: Array; + capabilities?: any; + failedCount?: number; + exitCode?: number; + specResults?: Array; } /** diff --git a/spec/basic/expected_conditions_spec.js b/spec/basic/expected_conditions_spec.js index bb1a14ea2..365d81a76 100644 --- a/spec/basic/expected_conditions_spec.js +++ b/spec/basic/expected_conditions_spec.js @@ -1,8 +1,9 @@ -const EC = protractor.ExpectedConditions; - describe('expected conditions', () => { + let EC = null; + beforeEach(async () => { await browser.get('index.html#/form'); + EC = protractor.ExpectedConditions; }); it('should have alertIsPresent', async () => {