Nicole Phillips
Nicole Phillips

Reputation: 763

Expect statement to read in array and then compare to a var array i declared

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

Answers (4)

Brine
Brine

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

BarretV
BarretV

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

Rahul Vig
Rahul Vig

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

Andres D
Andres D

Reputation: 8900

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'}, ...]
});

Upvotes: 0

Related Questions