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

Commit 528338c

Browse files
tilmanschweitzerheathkit
authored andcommitted
fix(expectedCondition): fix NoSuchElementError in visibilityOf due to a race condition (#3777)
Handle NoSuchElementError in the expected condition visibilityOf, which occurred when an element disappears between the isPresent() and isDisplayed() check.
1 parent 0872d96 commit 528338c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/expectedConditions.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,15 @@ export class ProtractorExpectedConditions {
388388
* representing whether the element is visible.
389389
*/
390390
visibilityOf(elementFinder: ElementFinder): Function {
391-
return this.and(this.presenceOf(elementFinder), elementFinder.isDisplayed.bind(elementFinder));
391+
return this.and(this.presenceOf(elementFinder), () => {
392+
return elementFinder.isDisplayed().then((displayed: boolean) => displayed, (err: any) => {
393+
if (err instanceof wderror.NoSuchElementError) {
394+
return false;
395+
} else {
396+
throw err;
397+
}
398+
});
399+
});
392400
}
393401

394402
/**

spec/basic/expected_conditions_spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ describe('expected conditions', function() {
4545
expect(visibilityOfHideable.call()).toBe(false);
4646
});
4747

48+
it('should have visibilityOf (handling race conditions)', function() {
49+
var disabledButton = $('#disabledButton[disabled="disabled"]');
50+
51+
// toggle presence (of .ng-hide) between visibility evaluation to simulate race condition
52+
var originalIsDisplayedFn = disabledButton.isDisplayed;
53+
disabledButton.isDisplayed = function () {
54+
element(by.model('disabled')).click();
55+
return originalIsDisplayedFn.call(this);
56+
};
57+
58+
var visibilityOfDisabledButtonWithInterceptor = EC.visibilityOf(disabledButton);
59+
60+
element(by.model('disabled')).click();
61+
62+
expect(originalIsDisplayedFn.call(disabledButton)).toBe(true);
63+
expect(disabledButton.isPresent()).toBe(true);
64+
65+
expect(visibilityOfDisabledButtonWithInterceptor.call()).toBe(false);
66+
});
67+
4868
it('should have invisibilityOf', function() {
4969
var invisibilityOfInvalid = EC.invisibilityOf($('#INVALID'));
5070
var invisibilityOfHideable = EC.invisibilityOf($('#shower'));

0 commit comments

Comments
 (0)