0

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.

Nicole Phillips
  • 753
  • 1
  • 18
  • 41

4 Answers4

1

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);
});
BarretV
  • 1,197
  • 7
  • 17
0

Did you try to use map?

myLists.map(function(row, index){
  return {
    text: row.getText(),
    name: roomsAsc[index]
  }
}).then(function(theValues){
  // You will get an array:
  // [{name: 'one', text: 'one text'}, ...]
});
Andres D
  • 8,910
  • 2
  • 26
  • 31
  • I did, however it will not scroll for me. so i get something that looks like this: ['one', 'two', 'three',' ', ' ', ', ' '] as the list does not get recorded until I scroll down. My above code will scroll down and print the list line by line to the console, however it only stores the elements not visible on the screen, which is bizarre to me. – Nicole Phillips Aug 12 '15 at 18:31
0

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); 
Community
  • 1
  • 1
Rahul Vig
  • 716
  • 1
  • 7
  • 24
0

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);
Brine
  • 3,733
  • 1
  • 21
  • 38