所以我逐行阅读元素列表。他们像这样登录到控制台:
one
two
three
我想要的是我的数组硬编码与文本进行逐行比较 所以期望看起来像:
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个在数组中是空白的,这是奇数,所以现在我试图逐行预期。
答案 0 :(得分:1)
我在.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);
});
答案 1 :(得分:0)
您是否尝试使用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'}, ...]
});
答案 2 :(得分:0)
您可能遇到与闭包有关的问题。从以下链接中了解有关闭包的更多信息 - Using protractor with loops
更好的解决方案是使用递归 -
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);
答案 3 :(得分:0)
我认为您的问题是异步循环。因为您的测试是异步的,所以它会立即触发循环,因此您的测试实际上是从最后一个循环开始的。因此,您的测试首先滚动到最后一个元素,然后仅返回那个可见的 。令人困惑,是的。
在那里:)我喜欢的解决方案是使用Immediately Invoked Function Expression(iife),你将索引传递给函数,一切都很好。
像...一样的东西。
roomsAsc = [&#39; one&#39;,&#39; two&#39;,&#39; three&#39;];
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);