This repository was archived by the owner on Jul 29, 2024. It is now read-only.
File tree 2 files changed +19
-7
lines changed
2 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -285,6 +285,7 @@ var buildElementHelper = function(ptor) {
285
285
286
286
/**
287
287
* 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)
288
289
* This does not actually retrieve the underlying element.
289
290
*
290
291
* @alias element.all(locator).get(index)
@@ -307,16 +308,17 @@ var buildElementHelper = function(ptor) {
307
308
var self = this ;
308
309
var getWebElements = function ( ) {
309
310
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 ;
313
315
}
314
- if ( index >= parentWebElements . length ) {
316
+ if ( i < 0 || i >= parentWebElements . length ) {
315
317
throw new Error ( 'Index out of bound. Trying to access element at ' +
316
318
'index:' + index + ', but there are only ' +
317
319
parentWebElements . length + ' elements' ) ;
318
320
}
319
- return [ parentWebElements [ index ] ] ;
321
+ return [ parentWebElements [ i ] ] ;
320
322
} ) ;
321
323
} ;
322
324
return new ElementArrayFinder ( getWebElements , this . locator_ ) . toElementFinder_ ( ) ;
Original file line number Diff line number Diff line change @@ -366,18 +366,28 @@ describe('ElementArrayFinder', function() {
366
366
expect ( colorList . get ( 2 ) . getAttribute ( 'value' ) ) . toEqual ( 'red' ) ;
367
367
} ) ;
368
368
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
+
369
379
it ( 'should get the first element from an array' , function ( ) {
370
380
var colorList = element . all ( by . model ( 'color' ) ) ;
371
381
browser . get ( 'index.html#/form' ) ;
372
382
373
- expect ( colorList . first ( 0 ) . getAttribute ( 'value' ) ) . toEqual ( 'blue' ) ;
383
+ expect ( colorList . first ( ) . getAttribute ( 'value' ) ) . toEqual ( 'blue' ) ;
374
384
} ) ;
375
385
376
386
it ( 'should get the last element from an array' , function ( ) {
377
387
var colorList = element . all ( by . model ( 'color' ) ) ;
378
388
browser . get ( 'index.html#/form' ) ;
379
389
380
- expect ( colorList . last ( 0 ) . getAttribute ( 'value' ) ) . toEqual ( 'red' ) ;
390
+ expect ( colorList . last ( ) . getAttribute ( 'value' ) ) . toEqual ( 'red' ) ;
381
391
} ) ;
382
392
383
393
it ( 'should perform an action on each element in an array' , function ( ) {
You can’t perform that action at this time.
0 commit comments