使用带有环的量角器
循环索引(i
)不是我在循环中使用Protractor时所期望的。
症状:
失败:索引越界。试图访问索引处的元素:'x',但只有'x'元素
要么
索引是静态的,始终等于最后一个值
我的代码
for (var i = 0; i < MAX; ++i) { getPromise().then(function() { someArray[i] // 'i' always takes the value of 'MAX' })}
例如:
var expected = ['expect1', 'expect2', 'expect3'];var els = element.all(by.css('selector'));for (var i = 0; i < expected.length; ++i) { els.get(i).getText().then(function(text) { expect(text).toEqual(expected[i]); // Error: `i` is always 3. })}
要么
var els = element.all(by.css('selector'));for (var i = 0; i < 3; ++i) { els.get(i).getText().then(function(text) { if (text === 'should click') { els.get(i).click(); // fails with "Failed: Index out of bound. Trying to access element at index:3, but there are only 3 elements" } })}
要么
var els = element.all(by.css('selector'));els.then(function(rawelements) { for (var i = 0; i < rawelements.length; ++i) { rawelements[i].getText().then(function(text) { if (text === 'should click') { rawelements[i].click(); // fails with "Failed: Index out of bound. Trying to access element at index:'rawelements.length', but there are only 'rawelements.length' elements" } }) }})
心有法竹
一只名叫tom的猫
相关分类