我是新的量角器测试,我一直在尝试将多个webelements中的文本值打印到控制台。我想使用“element.all”表示法将值存储在变量中,然后遍历数组并逐个打印文本。我已经尝试了很多不同的方法,并阅读有关承诺,并尝试通过.filter,.get,.each函数处理它们,但没有任何内容在控制台中打印
以下是我尝试的示例代码
// spec.js
describe('Protractor Demo App', function() {
var firstNumber = element(by.model('first'));
var secondNumber = element(by.model('second'));
var goButton = element(by.id('gobutton'));
var latestResult = element(by.binding('latest'));
var history = element.all(by.repeater('result in memory'))
function add (a, b){
firstNumber.sendKeys(a);
secondNumber.sendKeys(b);
goButton.click();
}
beforeEach(function() {
browser.get('http://juliemr.github.io/protractor-demo/');
});
it('should have a title', function() {
expect(browser.getTitle()).toEqual('Super Calculator');
});
it('should have a history', function() {
add(1, 2);
add(3,4);
expect(history.count()).toEqual(2);
add(5,6);
expect(history.count()).toEqual(3); // This is correct!
});
it('should have a history2', function() {
add(1,2);
add(3,4);
for (var i =0; i < history.lenght; i++){
history.get[i].getText().then(function (text){
console.log(text);
});
}
});
});
第二个'it'区块是我尝试这个的地方。任何帮助都将不胜感激
答案 0 :(得分:0)
// Option 1
function printHistory1() {
history.each(function(item){
item.getText().then(function(txt, index){
console.log('Add result['+(index+1)+'] = ' + txt);
})
});
}
// Option 2
function printHistory2() {
history.getText().then(function(results){
results.forEach(function(txt, index){
console.log('Add result['+(index+1)+'] = ' + txt);
});
})
}
it('should have a history', function() {
add(1, 2);
add(3,4);
printHistory1();
expect(history.count()).toEqual(2);
add(5,6);
printHistory2();
expect(history.count()).toEqual(3); // This is correct!
});
Third
阻止了
it('should have a history2', function() {
add(1,2);
add(3,4);
for (var i =0; i < history.length; i++) {
history.get[i].getText().then(function (text){
console.log(text);
});
}
// element.all() not has `length` property, you should use `count()` which also return a Pormise.
// the correct way to use `count()` to archive same purpose
history.count().then(function(count){
for (var i =0; i < count; i++) {
(function(index){
history.get[i].getText().then(function (text){
console.log(text);
});
}) // () returns a function, and () at here make javascript closure
(i); // (i) it's to call function with argument: i
}
});
});