Skip to content

Commit

Permalink
fix(expectedCondition): fix NoSuchElementError in visibilityOf due to…
Browse files Browse the repository at this point in the history
… a race condition (angular#3777)

Handle NoSuchElementError in the expected condition visibilityOf, which occurred when
an element disappears between the isPresent() and isDisplayed() check.
  • Loading branch information
tilmanschweitzer authored and igniteram committed Feb 21, 2017
1 parent 445e590 commit ace1681
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/expectedConditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,15 @@ export class ProtractorExpectedConditions {
* representing whether the element is visible.
*/
visibilityOf(elementFinder: ElementFinder): Function {
return this.and(this.presenceOf(elementFinder), elementFinder.isDisplayed.bind(elementFinder));
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;
}
});
});
}

/**
Expand Down
20 changes: 20 additions & 0 deletions spec/basic/expected_conditions_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ describe('expected conditions', function() {
expect(visibilityOfHideable.call()).toBe(false);
});

it('should have visibilityOf (handling race conditions)', function() {
var disabledButton = $('#disabledButton[disabled="disabled"]');

// toggle presence (of .ng-hide) between visibility evaluation to simulate race condition
var originalIsDisplayedFn = disabledButton.isDisplayed;
disabledButton.isDisplayed = function () {
element(by.model('disabled')).click();
return originalIsDisplayedFn.call(this);
};

var visibilityOfDisabledButtonWithInterceptor = EC.visibilityOf(disabledButton);

element(by.model('disabled')).click();

expect(originalIsDisplayedFn.call(disabledButton)).toBe(true);
expect(disabledButton.isPresent()).toBe(true);

expect(visibilityOfDisabledButtonWithInterceptor.call()).toBe(false);
});

it('should have invisibilityOf', function() {
var invisibilityOfInvalid = EC.invisibilityOf($('#INVALID'));
var invisibilityOfHideable = EC.invisibilityOf($('#shower'));
Expand Down

0 comments on commit ace1681

Please sign in to comment.