Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit de72230

Browse files
committed
chore(expectedConditions): update generic Function typings
- Use `() => Promise<boolean>` over `Function` typings. - Fix an ExpectedConditions test where it was set to a const. - Fix a TypeScript typing interface issue with RunResults in taskRunner. - Fix a browser call to use waitForAngularEnabled method over the private variable.
1 parent 7654039 commit de72230

File tree

4 files changed

+75
-75
lines changed

4 files changed

+75
-75
lines changed

lib/browser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
838838

839839
// Reset bpClient sync
840840
if (this.bpClient) {
841-
await this.bpClient.setWaitEnabled(!this.internalIgnoreSynchronization);
841+
await this.bpClient.setWaitEnabled(await this.waitForAngularEnabled());
842842
}
843843

844844
// Run Plugins

lib/expectedConditions.ts

+65-66
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ export class ProtractorExpectedConditions {
5959
*
6060
* @returns {!function} An expected condition that returns the negated value.
6161
*/
62-
not(expectedCondition: Function): Function {
63-
return (): Function => {
64-
return expectedCondition().then((bool: boolean): boolean => {
65-
return !bool;
66-
});
62+
not(expectedCondition: Function): (() => Promise<boolean>) {
63+
return async(): Promise<boolean> => {
64+
const bool = await expectedCondition();
65+
return !bool;
6766
};
6867
}
6968

@@ -78,20 +77,19 @@ export class ProtractorExpectedConditions {
7877
* @returns {!function} An expected condition that returns a promise which
7978
* evaluates to the result of the logical chain.
8079
*/
81-
logicalChain_(defaultRet: boolean, fns: Array<Function>): Function {
80+
logicalChain_(defaultRet: boolean, fns: Array<Function>): (() => Promise<boolean>) {
8281
let self = this;
83-
return (): boolean => {
82+
return async(): Promise<boolean> => {
8483
if (fns.length === 0) {
8584
return defaultRet;
8685
}
87-
let fn = fns[0];
88-
return fn().then((bool: boolean): boolean => {
89-
if (bool === defaultRet) {
90-
return self.logicalChain_(defaultRet, fns.slice(1))();
91-
} else {
92-
return !defaultRet;
93-
}
94-
});
86+
const fn = fns[0];
87+
const bool = await fn();
88+
if (bool === defaultRet) {
89+
return self.logicalChain_(defaultRet, fns.slice(1))();
90+
} else {
91+
return !defaultRet;
92+
}
9593
};
9694
}
9795

@@ -113,7 +111,7 @@ export class ProtractorExpectedConditions {
113111
* @returns {!function} An expected condition that returns a promise which
114112
* evaluates to the result of the logical and.
115113
*/
116-
and(...args: Function[]): Function {
114+
and(...args: Function[]): (() => Promise<boolean>) {
117115
return this.logicalChain_(true, args);
118116
}
119117

@@ -135,7 +133,7 @@ export class ProtractorExpectedConditions {
135133
* @returns {!function} An expected condition that returns a promise which
136134
* evaluates to the result of the logical or.
137135
*/
138-
or(...args: Function[]): Function {
136+
or(...args: Function[]): (() => Promise<boolean>) {
139137
return this.logicalChain_(false, args);
140138
}
141139

@@ -151,20 +149,18 @@ export class ProtractorExpectedConditions {
151149
* @returns {!function} An expected condition that returns a promise
152150
* representing whether an alert is present.
153151
*/
154-
alertIsPresent(): Function {
155-
return () => {
156-
return this.browser.driver.switchTo().alert().then(
157-
():
158-
boolean => {
159-
return true;
160-
},
161-
(err: any) => {
162-
if (err instanceof wderror.NoSuchAlertError) {
163-
return false;
164-
} else {
165-
throw err;
166-
}
167-
});
152+
alertIsPresent(): (() => Promise<boolean>) {
153+
return async(): Promise<boolean> => {
154+
try {
155+
await this.browser.driver.switchTo().alert();
156+
return true;
157+
} catch (e) {
158+
if (e instanceof wderror.NoSuchAlertError) {
159+
return false;
160+
} else {
161+
throw e;
162+
}
163+
}
168164
};
169165
}
170166

@@ -183,7 +179,7 @@ export class ProtractorExpectedConditions {
183179
* @returns {!function} An expected condition that returns a promise
184180
* representing whether the element is clickable.
185181
*/
186-
elementToBeClickable(elementFinder: ElementFinder): Function {
182+
elementToBeClickable(elementFinder: ElementFinder): (() => Promise<boolean>) {
187183
return this.and(this.visibilityOf(elementFinder), () => {
188184
return elementFinder.isEnabled().then(passBoolean, falseIfMissing);
189185
});
@@ -205,13 +201,16 @@ export class ProtractorExpectedConditions {
205201
* @returns {!function} An expected condition that returns a promise
206202
* representing whether the text is present in the element.
207203
*/
208-
textToBePresentInElement(elementFinder: ElementFinder, text: string): Function {
209-
let hasText = () => {
210-
return elementFinder.getText().then((actualText: string): boolean => {
204+
textToBePresentInElement(elementFinder: ElementFinder, text: string): (() => Promise<boolean>) {
205+
let hasText = async () => {
206+
try {
207+
const actualText = await elementFinder.getText();
211208
// MSEdge does not properly remove newlines, which causes false
212209
// negatives
213210
return actualText.replace(/\r?\n|\r/g, '').indexOf(text) > -1;
214-
}, falseIfMissing);
211+
} catch (e) {
212+
falseIfMissing(e);
213+
}
215214
};
216215
return this.and(this.presenceOf(elementFinder), hasText);
217216
}
@@ -232,11 +231,15 @@ export class ProtractorExpectedConditions {
232231
* @returns {!function} An expected condition that returns a promise
233232
* representing whether the text is present in the element's value.
234233
*/
235-
textToBePresentInElementValue(elementFinder: ElementFinder, text: string): Function {
236-
let hasText = () => {
237-
return elementFinder.getAttribute('value').then((actualText: string): boolean => {
234+
textToBePresentInElementValue(elementFinder: ElementFinder, text: string):
235+
(() => Promise<boolean>) {
236+
let hasText = async () => {
237+
try {
238+
const actualText = await elementFinder.getAttribute('value');
238239
return actualText.indexOf(text) > -1;
239-
}, falseIfMissing);
240+
} catch (e) {
241+
falseIfMissing(e);
242+
}
240243
};
241244
return this.and(this.presenceOf(elementFinder), hasText);
242245
}
@@ -256,11 +259,10 @@ export class ProtractorExpectedConditions {
256259
* @returns {!function} An expected condition that returns a promise
257260
* representing whether the title contains the string.
258261
*/
259-
titleContains(title: string): Function {
260-
return () => {
261-
return this.browser.driver.getTitle().then((actualTitle: string): boolean => {
262-
return actualTitle.indexOf(title) > -1;
263-
});
262+
titleContains(title: string): (() => Promise<boolean>) {
263+
return async(): Promise<boolean> => {
264+
const actualTitle = await this.browser.driver.getTitle();
265+
return actualTitle.indexOf(title) > -1;
264266
};
265267
}
266268

@@ -278,12 +280,11 @@ export class ProtractorExpectedConditions {
278280
* @returns {!function} An expected condition that returns a promise
279281
* representing whether the title equals the string.
280282
*/
281-
titleIs(title: string): Function {
282-
return () => {
283-
return this.browser.driver.getTitle().then((actualTitle: string): boolean => {
284-
return actualTitle === title;
285-
});
286-
};
283+
titleIs(title: string): (() => Promise<boolean>) {
284+
return async(): Promise<boolean> => {
285+
const actualTitle = await this.browser.driver.getTitle();
286+
return actualTitle === title;
287+
}
287288
}
288289

289290
/**
@@ -301,11 +302,10 @@ export class ProtractorExpectedConditions {
301302
* @returns {!function} An expected condition that returns a promise
302303
* representing whether the URL contains the string.
303304
*/
304-
urlContains(url: string): Function {
305-
return () => {
306-
return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => {
307-
return actualUrl.indexOf(url) > -1;
308-
});
305+
urlContains(url: string): (() => Promise<boolean>) {
306+
return async(): Promise<boolean> => {
307+
const actualUrl = await this.browser.driver.getCurrentUrl();
308+
return actualUrl.indexOf(url) > -1;
309309
};
310310
}
311311

@@ -323,11 +323,10 @@ export class ProtractorExpectedConditions {
323323
* @returns {!function} An expected condition that returns a promise
324324
* representing whether the url equals the string.
325325
*/
326-
urlIs(url: string): Function {
327-
return () => {
328-
return this.browser.driver.getCurrentUrl().then((actualUrl: string): boolean => {
329-
return actualUrl === url;
330-
});
326+
urlIs(url: string): (() => Promise<boolean>) {
327+
return async(): Promise<boolean> => {
328+
const actualUrl = await this.browser.driver.getCurrentUrl();
329+
return actualUrl === url;
331330
};
332331
}
333332

@@ -347,7 +346,7 @@ export class ProtractorExpectedConditions {
347346
* @returns {!function} An expected condition that returns a promise
348347
* representing whether the element is present.
349348
*/
350-
presenceOf(elementFinder: ElementFinder): Function {
349+
presenceOf(elementFinder: ElementFinder): (() => Promise<boolean>) {
351350
return elementFinder.isPresent.bind(elementFinder);
352351
}
353352

@@ -366,7 +365,7 @@ export class ProtractorExpectedConditions {
366365
* @returns {!function} An expected condition that returns a promise
367366
* representing whether the element is stale.
368367
*/
369-
stalenessOf(elementFinder: ElementFinder): Function {
368+
stalenessOf(elementFinder: ElementFinder): (() => Promise<boolean>) {
370369
return this.not(this.presenceOf(elementFinder));
371370
}
372371

@@ -388,7 +387,7 @@ export class ProtractorExpectedConditions {
388387
* @returns {!function} An expected condition that returns a promise
389388
* representing whether the element is visible.
390389
*/
391-
visibilityOf(elementFinder: ElementFinder): Function {
390+
visibilityOf(elementFinder: ElementFinder): (() => Promise<boolean>) {
392391
return this.and(this.presenceOf(elementFinder), () => {
393392
return elementFinder.isDisplayed().then(passBoolean, falseIfMissing);
394393
});
@@ -409,7 +408,7 @@ export class ProtractorExpectedConditions {
409408
* @returns {!function} An expected condition that returns a promise
410409
* representing whether the element is invisible.
411410
*/
412-
invisibilityOf(elementFinder: ElementFinder): Function {
411+
invisibilityOf(elementFinder: ElementFinder): (() => Promise<boolean>) {
413412
return this.not(this.visibilityOf(elementFinder));
414413
}
415414

@@ -427,7 +426,7 @@ export class ProtractorExpectedConditions {
427426
* @returns {!function} An expected condition that returns a promise
428427
* representing whether the element is selected.
429428
*/
430-
elementToBeSelected(elementFinder: ElementFinder): Function {
429+
elementToBeSelected(elementFinder: ElementFinder): (() => Promise<boolean>) {
431430
return this.and(this.presenceOf(elementFinder), () => {
432431
return elementFinder.isSelected().then(passBoolean, falseIfMissing);
433432
});

lib/taskRunner.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {Runner} from './runner';
77
import {TaskLogger} from './taskLogger';
88

99
export interface RunResults {
10-
taskId: number;
11-
specs: Array<string>;
12-
capabilities: any;
13-
failedCount: number;
14-
exitCode: number;
15-
specResults: Array<any>;
10+
taskId?: number;
11+
specs?: Array<string>;
12+
capabilities?: any;
13+
failedCount?: number;
14+
exitCode?: number;
15+
specResults?: Array<any>;
1616
}
1717

1818
/**

spec/basic/expected_conditions_spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const EC = protractor.ExpectedConditions;
2-
31
describe('expected conditions', () => {
2+
let EC = null;
3+
44
beforeEach(async () => {
55
await browser.get('index.html#/form');
6+
EC = protractor.ExpectedConditions;
67
});
78

89
it('should have alertIsPresent', async () => {

0 commit comments

Comments
 (0)