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

Commit

Permalink
fix(element): isPresent should not throw on chained finders
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
juliemr committed Sep 9, 2014
1 parent 466b383 commit 838f5a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ var buildElementHelper = function(ptor) {
ElementArrayFinder.prototype.count = function() {
return this.getWebElements().then(function(arr) {
return arr.length;
}, function(err) {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
return 0;
} else {
throw err;
}
});
};

Expand Down Expand Up @@ -682,8 +688,10 @@ var buildElementHelper = function(ptor) {
var getWebElements = function() {
return elementArrayFinder.getWebElements().then(function(webElements) {
if (webElements.length === 0) {
throw new Error('No element found using locator: ' +
elementArrayFinder.locator_.toString());
throw new webdriver.error.Error(
webdriver.error.ErrorCode.NO_SUCH_ELEMENT,
'No element found using locator: ' +
elementArrayFinder.locator_.toString());
} else {
if (webElements.length > 1) {
console.log('warning: more than one element found for locator ' +
Expand Down
28 changes: 28 additions & 0 deletions spec/basic/elements_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ describe('ElementFinder', function() {
expect(element(by.binding('nopenopenope')).isPresent()).toBe(false);
});

it('should allow handling errors', function() {
browser.get('index.html#/form');
var elmFinder = $('.nopenopenope').getText().then(function(success) {
// This should throw an error. Fail.
expect(true).toEqual(false);
}, function(err) {
expect(true).toEqual(true);
});
});

it('should allow handling chained errors', function() {
browser.get('index.html#/form');
var elmFinder = $('.nopenopenope').$('furthernope').getText().then(
function(success) {
// This should throw an error. Fail.
expect(true).toEqual(false);
}, function(err) {
expect(true).toEqual(true);
});
});

it('isPresent() should not raise error on chained finders', function() {
browser.get('index.html#/form');
var elmFinder = $('.nopenopenope').element(by.binding('greet'));

expect(elmFinder.isPresent()).toBe(false);
});

it('should export an allowAnimations helper', function() {
browser.get('index.html#/animation');
var animationTop = element(by.id('animationTop'));
Expand Down

0 comments on commit 838f5a2

Please sign in to comment.