当我尝试使用量角器进行迭代时,我遇到了麻烦。这是我的代码:
var accountNumber = 2;
var accountsInfo = [{name: 'Hugo Marin', bank: 'Banco Capital', accountNumber: '43543'},
{name: 'Hugo Marin', bank: 'Banco de Loja', accountNumber: '12345'}];
var accounts = element.all(by.repeater('bankAccount in bankPersonCtrl.otherBanksAccounts'));
expect(accounts.count()).toBe(accountNumber);
for (var i = 0; i < accountNumber; i++) {
accounts.get(i).getText().then(function(text) {
text = text.split('\n');
var name = text[0];
var bank = text[1];
var accountNumber = text[2];
console.log('i is: ' + i);
expect(name).toEqual(accountsInfo[i].name);
expect(bank).toEqual(accountsInfo[i].bank);
expect(accountNumber).toEqual(accountsInfo[i].accountNumber);
});
}
我认为逻辑是正确的,但我得到了
Failed: Cannot read property 'name' of undefined
此外,不要进入console.log('i is:' + i);
==&gt; i is: 0
,我正在 i is: 2
。
我尝试做的是将每个元素放在转发器中,这样我就可以比较它们的文本。
答案 0 :(得分:2)
阅读量角器文档后,这解决了我的问题:
var accountNumber = 2;
var accountsInfo = [{name: 'Hugo Marin', bank: 'Banco Capital', accountNumber: '43543'},
{name: 'Hugo Marin', bank: 'Banco de Loja', accountNumber: '12345'}];
var accounts = element.all(by.repeater('bankAccount in bankPersonCtrl.otherBanksAccounts'));
expect(accounts.count()).toBe(accountNumber);
accounts.each( function(element, index) {
element.getText().then(function(text) {
text = text.split('\n');
expect(text[0]).toEqual(accountsInfo[index].name);
expect(text[1]).toEqual(accountsInfo[index].bank);
expect(text[2]).toEqual(accountsInfo[index].accountNumber);
});
});
使用each是解决方案,因为我可以拥有每个元素的索引。