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

Commit 838f5a2

Browse files
committed
fix(element): isPresent should not throw on chained finders
Now, `$('nonexistant').$('foo').isPresent()` will return false instead of throwing an error. This change also adds tests that ensure that catching errors from promises works as expected.
1 parent 466b383 commit 838f5a2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/protractor.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ var buildElementHelper = function(ptor) {
393393
ElementArrayFinder.prototype.count = function() {
394394
return this.getWebElements().then(function(arr) {
395395
return arr.length;
396+
}, function(err) {
397+
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
398+
return 0;
399+
} else {
400+
throw err;
401+
}
396402
});
397403
};
398404

@@ -682,8 +688,10 @@ var buildElementHelper = function(ptor) {
682688
var getWebElements = function() {
683689
return elementArrayFinder.getWebElements().then(function(webElements) {
684690
if (webElements.length === 0) {
685-
throw new Error('No element found using locator: ' +
686-
elementArrayFinder.locator_.toString());
691+
throw new webdriver.error.Error(
692+
webdriver.error.ErrorCode.NO_SUCH_ELEMENT,
693+
'No element found using locator: ' +
694+
elementArrayFinder.locator_.toString());
687695
} else {
688696
if (webElements.length > 1) {
689697
console.log('warning: more than one element found for locator ' +

spec/basic/elements_spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ describe('ElementFinder', function() {
9191
expect(element(by.binding('nopenopenope')).isPresent()).toBe(false);
9292
});
9393

94+
it('should allow handling errors', function() {
95+
browser.get('index.html#/form');
96+
var elmFinder = $('.nopenopenope').getText().then(function(success) {
97+
// This should throw an error. Fail.
98+
expect(true).toEqual(false);
99+
}, function(err) {
100+
expect(true).toEqual(true);
101+
});
102+
});
103+
104+
it('should allow handling chained errors', function() {
105+
browser.get('index.html#/form');
106+
var elmFinder = $('.nopenopenope').$('furthernope').getText().then(
107+
function(success) {
108+
// This should throw an error. Fail.
109+
expect(true).toEqual(false);
110+
}, function(err) {
111+
expect(true).toEqual(true);
112+
});
113+
});
114+
115+
it('isPresent() should not raise error on chained finders', function() {
116+
browser.get('index.html#/form');
117+
var elmFinder = $('.nopenopenope').element(by.binding('greet'));
118+
119+
expect(elmFinder.isPresent()).toBe(false);
120+
});
121+
94122
it('should export an allowAnimations helper', function() {
95123
browser.get('index.html#/animation');
96124
var animationTop = element(by.id('animationTop'));

0 commit comments

Comments
 (0)