期望语句读取数组,然后与我声明的var数组进行比较



所以我正在逐行阅读元素列表。它们像这样被记录到控制台:

one
two
three

我想要的是我的数组硬编码与文本逐行比较所以expect看起来像:

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);
});

上面的代码滚动,直到所有元素的列表都是可见的。名单上有28个。我将它们全部打印到控制台,但是,只有不可查看的元素被存储,前13个元素在数组中是空白的,这是奇数,所以现在我尝试逐行期望。

我在.then()函数中使用for循环中的迭代器时遇到了麻烦。所以我声明了另一个变量来迭代另一个数组,并在.then()函数中进行自增。看看这是否能给你带来更好的结果

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);
});

是否尝试使用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'}, ...]
});

您可能面临与闭包相关的问题。从下面的链接阅读更多关于闭包的内容-使用带有循环的量角器

一个更好的解决方案是使用递归-
 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); 

我认为你的问题是异步循环。因为你的测试是异步的,它立即通过循环触发,所以你的测试实际上从最后一个循环开始。因此,您的测试从滚动到最后一个元素开始,并且只返回在该点处可见的。令人困惑的,是的。

Been there:)我喜欢的解决方案是使用一个立即调用的函数表达式(iife),你通过你的索引传递给函数,一切都很好。

之类的…

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);

最新更新