Skip to content

Commit

Permalink
fix(ExpectedConditions): improve missing element handling
Browse files Browse the repository at this point in the history
Add error handling for missing elements to fix race condition test cases.

angular#3972
  • Loading branch information
tilmanschweitzer committed Jan 17, 2017
1 parent e4735d0 commit e27c035
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
public browser_: ProtractorBrowser,
public getWebElements: () => wdpromise.Promise<WebElement[]> = null, public locator_?: any,
public actionResults_: wdpromise.Promise<any> = null,
public falseIfMissing_: boolean = false) {
public isFalseIfMissing: boolean = false) {
super();

// TODO(juliemr): might it be easier to combine this with our docs and just
Expand Down Expand Up @@ -465,7 +465,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
// map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
private applyAction_(
actionFn: (value: WebElement, index: number, array: WebElement[]) => any,
falseIfMissing?: boolean): ElementArrayFinder {
isFalseIfMissing?: boolean): ElementArrayFinder {
let callerError = new Error();
let actionResults = this.getWebElements()
.then((arr: any) => wdpromise.all(arr.map(actionFn)))
Expand All @@ -480,8 +480,11 @@ export class ElementArrayFinder extends WebdriverWebElement {
}
throw noSuchErr;
});
if (isFalseIfMissing) {
actionResults = actionResults.then(null, falseIfMissing);
}
return new ElementArrayFinder(
this.browser_, this.getWebElements, this.locator_, actionResults, falseIfMissing);
this.browser_, this.getWebElements, this.locator_, actionResults, isFalseIfMissing);
}

/**
Expand Down Expand Up @@ -808,22 +811,12 @@ 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(
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);
return this.elementArrayFinder_.then((actionResults: any) => {
if (!fn) {
return actionResults[0];
}
return fn(actionResults[0]);
}, errorFn);
};
}

Expand Down

0 comments on commit e27c035

Please sign in to comment.