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

Commit

Permalink
feat(element): element.all now has 'first' and 'last' methods
Browse files Browse the repository at this point in the history
Return the first or last element that matches a locator. Closes #171.
  • Loading branch information
juliemr committed Nov 22, 2013
1 parent ef61662 commit ce5f494
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ var buildElementHelper = function(ptor) {
return new webdriver.WebElement(ptor.driver, id);
};

elementArrayFinder.first = function() {
var id = ptor.findElements(locator).then(function(arr) {
if (!arr.length) {
throw new Error('No element found using locator: ' + locator.message);
}
return arr[0];
});
return new webdriver.WebElement(ptor.driver, id);
};

elementArrayFinder.last = function() {
var id = ptor.findElements(locator).then(function(arr) {
return arr[arr.length - 1];
});
return new webdriver.WebElement(ptor.driver, id);
};

elementArrayFinder.then = function(fn) {
return ptor.findElements(locator).then(fn);
};
Expand Down
14 changes: 14 additions & 0 deletions spec/basic/findelements_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ describe('global element function', function() {
expect(colorList.get(2).getAttribute('value')).toEqual('red');
});

it('should get the first element from an array', function() {
var colorList = element.all(by.model('color'));
browser.get('index.html#/form');

expect(colorList.first(0).getAttribute('value')).toEqual('blue');
});

it('should get the last element from an array', function() {
var colorList = element.all(by.model('color'));
browser.get('index.html#/form');

expect(colorList.last(0).getAttribute('value')).toEqual('red');
});

it('should export an isPresent helper', function() {
expect(element(by.binding('greet')).isPresent()).toBe(true);
expect(element(by.binding('nopenopenope')).isPresent()).toBe(false);
Expand Down

0 comments on commit ce5f494

Please sign in to comment.