Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using protractor with loops

Loop index (i) is not what I'm expecting when I use Protractor within a loop.

Symptoms:

Failed: Index out of bound. Trying to access element at index:'x', but there are only 'x' elements

or

Index is static and always equal to the last value

My code

for (var i = 0; i < MAX; ++i) {   getPromise().then(function() {     someArray[i] // 'i' always takes the value of 'MAX'   }) } 

For example:

var expected = ['expect1', 'expect2', 'expect3']; var els = element.all(by.css('selector')); for (var i = 0; i < expected.length; ++i) {   els.get(i).getText().then(function(text) {     expect(text).toEqual(expected[i]); // Error: `i` is always 3.    }) } 

or

var els = element.all(by.css('selector')); for (var i = 0; i < 3; ++i) {   els.get(i).getText().then(function(text) {     if (text === 'should click') {       els.get(i).click(); // fails with "Failed: Index out of bound. Trying to access element at index:3, but there are only 3 elements"     }   }) } 

or

var els = element.all(by.css('selector')); els.then(function(rawelements) {   for (var i = 0; i < rawelements.length; ++i) {     rawelements[i].getText().then(function(text) {       if (text === 'should click') {         rawelements[i].click(); // fails with "Failed: Index out of bound. Trying to access element at index:'rawelements.length', but there are only 'rawelements.length' elements"       }     })   } }) 
like image 318
hankduan Avatar asked Jan 12 '15 20:01

hankduan


Video Answer


1 Answers

The reason this is happening is because protractor uses promises.

Read https://github.com/angular/protractor/blob/master/docs/control-flow.md

Promises (i.e. element(by...), element.all(by...)) execute their then functions when the underlying value becomes ready. What this means is that all the promises are first scheduled and then the then functions are run as the results become ready.

When you run something like this:

for (var i = 0; i < 3; ++i) {   console.log('1) i is: ', i);   getPromise().then(function() {     console.log('2) i is: ', i);     someArray[i] // 'i' always takes the value of 3   }) } console.log('*  finished looping. i is: ', i); 

What happens is that getPromise().then(function() {...}) returns immediately, before the promise is ready and without executing the function inside the then. So first the loop runs through 3 times, scheduling all the getPromise() calls. Then, as the promises resolve, the corresponding thens are run.

The console would look something like this:

1) i is: 0 // schedules first `getPromise()` 1) i is: 1 // schedules second `getPromise()` 1) i is: 2 // schedules third `getPromise()` *  finished looping. i is: 3 2) i is: 3 // first `then` function runs, but i is already 3 now. 2) i is: 3 // second `then` function runs, but i is already 3 now. 2) i is: 3 // third `then` function runs, but i is already 3 now. 

So, how do you run protractor in loops? The general solution is closure. See JavaScript closure inside loops – simple practical example

for (var i = 0; i < 3; ++i) {   console.log('1) i is: ', i);   var func = (function() {     var j = i;      return function() {       console.log('2) j is: ', j);       someArray[j] // 'j' takes the values of 0..2     }   })();   getPromise().then(func); } console.log('*  finished looping. i is: ', i); 

But this is not that nice to read. Fortunately, you can also use protractor functions filter(fn), get(i), first(), last(), and the fact that expect is patched to take promises, to deal with this.

Going back to the examples provided earlier. The first example can be rewritten as:

var expected = ['expect1', 'expect2', 'expect3']; var els = element.all(by.css('selector')); for (var i = 0; i < expected.length; ++i) {   expect(els.get(i).getText()).toEqual(expected[i]); // note, the i is no longer in a `then` function and take the correct values. } 

The second and third example can be rewritten as:

var els = element.all(by.css('selector')); els.filter(function(elem) {   return elem.getText().then(function(text) {     return text === 'should click';   }); }).click();  // note here we first used a 'filter' to select the appropriate elements, and used the fact that actions like `click` can act on an array to click all matching elements. The result is that we can stop using a for loop altogether.  

In other words, protractor has many ways to iterate or access element i so that you don't need to use for loops and i. But if you must use for loops and i, you can use the closure solution.

like image 86
hankduan Avatar answered Sep 19 '22 19:09

hankduan