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

fix(ExpectedConditions): allow ExpectedConditions to handle missing e… #3972

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ElementHelper, ProtractorBrowser} from './browser';
import {IError} from './exitCodes';
import {Locator} from './locators';
import {Logger} from './logger';
import {falseIfMissing} from './util';

let clientSideScripts = require('./clientsidescripts');

Expand All @@ -17,6 +18,8 @@ let WEB_ELEMENT_FUNCTIONS = [
'getLocation', 'isEnabled', 'isSelected', 'submit', 'clear', 'isDisplayed', 'getId', 'serialize',
'takeScreenshot'
] as (keyof WebdriverWebElement)[];
let FALSE_IF_MISSING_WEB_ELEMENT_FUNCTIONS =
['isEnabled', 'isSelected', 'isDisplayed'] as (keyof WebdriverWebElement)[];

/**
* ElementArrayFinder is used for operations on an array of elements (as opposed
Expand Down Expand Up @@ -82,7 +85,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
constructor(
public browser_: ProtractorBrowser,
public getWebElements: () => wdpromise.Promise<WebElement[]> = null, public locator_?: any,
public actionResults_: wdpromise.Promise<any> = null) {
public actionResults_: wdpromise.Promise<any> = null,
public falseIfMissing_: boolean = false) {
super();

// TODO(juliemr): might it be easier to combine this with our docs and just
Expand All @@ -92,7 +96,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
let actionFn = (webElem: any) => {
return webElem[fnName].apply(webElem, args);
};
return this.applyAction_(actionFn);
return this.applyAction_(
actionFn, FALSE_IF_MISSING_WEB_ELEMENT_FUNCTIONS.indexOf(fnName) !== -1);
};
});
}
Expand Down Expand Up @@ -458,8 +463,9 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @private
*/
// map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
private applyAction_(actionFn: (value: WebElement, index: number, array: WebElement[]) => any):
ElementArrayFinder {
private applyAction_(
actionFn: (value: WebElement, index: number, array: WebElement[]) => any,
falseIfMissing?: boolean): ElementArrayFinder {
let callerError = new Error();
let actionResults = this.getWebElements()
.then((arr: any) => wdpromise.all(arr.map(actionFn)))
Expand All @@ -474,7 +480,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
}
throw noSuchErr;
});
return new ElementArrayFinder(this.browser_, this.getWebElements, this.locator_, actionResults);
return new ElementArrayFinder(
this.browser_, this.getWebElements, this.locator_, actionResults, falseIfMissing);
}

/**
Expand Down Expand Up @@ -801,12 +808,22 @@ export class ElementFinder extends WebdriverWebElement {
// Access the underlying actionResult of ElementFinder.
this.then =
(fn: (value: any) => any | wdpromise.IThenable<any>, errorFn?: (error: any) => any) => {
return this.elementArrayFinder_.then((actionResults: any) => {
if (!fn) {
return actionResults[0];
}
return fn(actionResults[0]);
}, errorFn);
return this.elementArrayFinder_
.then(
null,
(error) => {
if (this.elementArrayFinder_.falseIfMissing_) {
return falseIfMissing(error);
} else {
throw error;
}
})
.then((actionResults: any) => {
if (!fn) {
return actionResults[0];
}
return fn(actionResults[0]);
}, errorFn);
};
}

Expand Down Expand Up @@ -1055,30 +1072,14 @@ export class ElementFinder extends WebdriverWebElement {
* the element is present on the page.
*/
isPresent(): wdpromise.Promise<boolean> {
return this.parentElementArrayFinder.getWebElements().then(
(arr: any[]) => {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(
() => {
return true; // is present, whether it is enabled or not
},
(err: any) => {
if (err instanceof wderror.StaleElementReferenceError) {
return false;
} else {
throw err;
}
});
},
(err: Error) => {
if (err instanceof wderror.NoSuchElementError) {
return false;
} else {
throw err;
}
});
return this.parentElementArrayFinder.getWebElements().then((arr: any[]) => {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(() => {
return true; // is present, whether it is enabled or not
});
}, falseIfMissing);
}

/**
Expand Down
15 changes: 4 additions & 11 deletions lib/expectedConditions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {error as wderror} from 'selenium-webdriver';
import {ProtractorBrowser} from './browser';
import {ElementFinder} from './element';
import {falseIfMissing} from './util';

/**
* Represents a library of canned expected conditions that are useful for
Expand Down Expand Up @@ -210,7 +211,7 @@ export class ProtractorExpectedConditions {
// MSEdge does not properly remove newlines, which causes false
// negatives
return actualText.replace(/\r?\n|\r/g, '').indexOf(text) > -1;
});
}, falseIfMissing);
};
return this.and(this.presenceOf(elementFinder), hasText);
}
Expand All @@ -235,7 +236,7 @@ export class ProtractorExpectedConditions {
let hasText = () => {
return elementFinder.getAttribute('value').then((actualText: string): boolean => {
return actualText.indexOf(text) > -1;
});
}, falseIfMissing);
};
return this.and(this.presenceOf(elementFinder), hasText);
}
Expand Down Expand Up @@ -388,15 +389,7 @@ export class ProtractorExpectedConditions {
* representing whether the element is visible.
*/
visibilityOf(elementFinder: ElementFinder): Function {
return this.and(this.presenceOf(elementFinder), () => {
return elementFinder.isDisplayed().then((displayed: boolean) => displayed, (err: any) => {
if (err instanceof wderror.NoSuchElementError) {
return false;
} else {
throw err;
}
});
});
return this.and(this.presenceOf(elementFinder), elementFinder.isDisplayed.bind(elementFinder));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {resolve} from 'path';
import {Promise, when} from 'q';
import {error as wderror} from 'selenium-webdriver';

let STACK_SUBSTRINGS_TO_FILTER = [
'node_modules/jasmine/', 'node_modules/selenium-webdriver', 'at Module.', 'at Object.Module.',
Expand Down Expand Up @@ -75,3 +76,21 @@ export function joinTestLogs(log1: any, log2: any): any {
specResults: (log1.specResults || []).concat(log2.specResults || [])
};
}

/**
* Returns false if an error indicates a missing or stale element, re-throws
* the error otherwise
*
* @param {*} The error to check
* @throws {*} The error it was passed if it doesn't indicate a missing or stale
* element
* @return {boolean} false, if it doesn't re-throw the error
*/
export function falseIfMissing(error: any) {
if ((error instanceof wderror.NoSuchElementError) ||
(error instanceof wderror.StaleElementReferenceError)) {
return false;
} else {
throw error;
}
}