Reputation: 763
So I am reading in a list of elements line by line. They are logged to console like this:
one
two
three
What I would like is my array hard coded with the text to compare line by line so the expect would look like:
one = one
two = two
three = three
roomsAsc = ['one', 'two', 'three'];
for (var i = 0; i < count; i++) {
//scrolls down the list element by element
browser.executeScript("arguments[0].scrollIntoView();", MyLists.get(i).getWebElement());
myLists.get(i).getText().then(function(text) {
//trying to get my array line be line like as java would
expect(text).toEqual(roomsAsc[i]);
//this says undefined in output
console.log(roomsAsc + 'array');
console.log(text);
});
}
//expect(myLists).toEqual(roomsAsc);
});
The code above scrolls until the list of all the elements are viewable. There are 28 in the list. I have them all printing to the console, however, only the non-viewable elements are being stored, the first 13 are blank in the array which is odd, so now I'm attempting to expects line by line.
Upvotes: 0
Views: 68
Reputation: 3731
I think your issue is async looping. Because your test is async, it's firing through the loop immediately, so your test actually starts with the last loop. Thus, your test starts by scrolling to the last element, and returning only those visible at that point. Confusing, yes.
Been there :) The solution I like is to use an Immediately Invoked Function Expression (iife), whereby you pass in your index to the function, and all is well.
Something like...
roomsAsc = ['one', 'two', 'three'];
for (var i = 0; i < count; i++) {
(function(i) {
//scrolls down the list element by element
browser.executeScript("arguments[0].scrollIntoView();", MyLists.get(i).getWebElement());
myLists.get(i).getText().then(function(text) {
//trying to get my array line be line like as java would
expect(text).toEqual(roomsAsc[i]);
//this says undefined in output
console.log(roomsAsc + 'array');
console.log(text);
});
})(i);
}
//expect(myLists).toEqual(roomsAsc);
Upvotes: 0
Reputation: 1197
I've had trouble using iterators from the for loop within a .then() function. So i've declared another variable to iterate through the other array and do the incrementing within the .then() function. See if this gives you any better results
roomsAsc = ['one', 'two', 'three'];
var j = 0; // using this since the i iterator in the for loop doesn't work within a then function
for (var i = 0; i < count; i++) {
//scrolls down the list element by element
browser.executeScript("arguments[0].scrollIntoView();", MyLists.get(i).getWebElement());
myLists.get(i).getText().then(function(text) {
//trying to get my array line be line like as java would
expect(text).toEqual(roomsAsc[j++]);
//this says undefined in output
console.log(roomsAsc + 'array');
console.log(text);
});
}
//expect(myLists).toEqual(roomsAsc);
});
Upvotes: 1
Reputation: 736
You could be facing a problem related to closures. Read more about closures from the following link- Using protractor with loops
A better solution will be to use recursion-
function loop(i){
if(i>count)
{
return null;
}
else
{
//scrolls down the list element by element
browser.executeScript("arguments[0].scrollIntoView();",MyLists.get(i).getWebElement());
myLists.get(i).getText().then(function(text) {
//trying to get my array line be line like as java would
expect(text).toEqual(roomsAsc[i]);
//this says undefined in output
console.log(roomsAsc + 'array');
console.log(text);
})
return loop(i+1)
}
}return loop(0);
Upvotes: 0