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

Commit 8dd60b7

Browse files
committed
feat(protractor): wrap negative indices for ElementArrayFinder.get(i)
Closes #1213
1 parent a89d666 commit 8dd60b7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/protractor.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ var buildElementHelper = function(ptor) {
285285

286286
/**
287287
* Get an element within the ElementArrayFinder by index. The index starts at 0.
288+
* Negative indices are wrapped (i.e. -i means ith element from last)
288289
* This does not actually retrieve the underlying element.
289290
*
290291
* @alias element.all(locator).get(index)
@@ -307,16 +308,17 @@ var buildElementHelper = function(ptor) {
307308
var self = this;
308309
var getWebElements = function() {
309310
return self.getWebElements().then(function(parentWebElements) {
310-
if (index === -1) {
311-
// -1 is special and means last
312-
index = parentWebElements.length - 1;
311+
var i = index;
312+
if (i < 0) {
313+
// wrap negative indices
314+
i = parentWebElements.length + i;
313315
}
314-
if (index >= parentWebElements.length) {
316+
if (i < 0 || i >= parentWebElements.length) {
315317
throw new Error('Index out of bound. Trying to access element at ' +
316318
'index:' + index + ', but there are only ' +
317319
parentWebElements.length + ' elements');
318320
}
319-
return [parentWebElements[index]];
321+
return [parentWebElements[i]];
320322
});
321323
};
322324
return new ElementArrayFinder(getWebElements, this.locator_).toElementFinder_();

spec/basic/elements_spec.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,28 @@ describe('ElementArrayFinder', function() {
366366
expect(colorList.get(2).getAttribute('value')).toEqual('red');
367367
});
368368

369+
it('should get an element from an array using negative indices', function() {
370+
var colorList = element.all(by.model('color'));
371+
372+
browser.get('index.html#/form');
373+
374+
expect(colorList.get(-3).getAttribute('value')).toEqual('blue');
375+
expect(colorList.get(-2).getAttribute('value')).toEqual('green');
376+
expect(colorList.get(-1).getAttribute('value')).toEqual('red');
377+
});
378+
369379
it('should get the first element from an array', function() {
370380
var colorList = element.all(by.model('color'));
371381
browser.get('index.html#/form');
372382

373-
expect(colorList.first(0).getAttribute('value')).toEqual('blue');
383+
expect(colorList.first().getAttribute('value')).toEqual('blue');
374384
});
375385

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

380-
expect(colorList.last(0).getAttribute('value')).toEqual('red');
390+
expect(colorList.last().getAttribute('value')).toEqual('red');
381391
});
382392

383393
it('should perform an action on each element in an array', function() {

0 commit comments

Comments
 (0)