我无法使用CSS或中继器获取所有元素。
this.clickRandomEmployee = function(){
employees = elementController.getAllElements('css','#employee-list li');
numberOfEmployees = employees.length;
console.log(numberOfEmployees);
var numRand = this.getRandomNumber(numberOfEmployees);
console.log(numRand);
elementController.doubleClick(employees.get(numRand));
}
this.getAllElements = function(locatorType,value){
var emps;
console.log(locatorType);
console.log(value);
if(locatorType=='css'){
emps = element.all(by.css(value));
}
else if(locatorType=='repeater'){
emps = element.all(by.repeater(value));
}
return emps;
};
从测试脚本调用以上代码以查找所有元素,但返回未定义。请建议!
最佳答案
getAllElements
返回一个承诺,该承诺将分解为元素数组。许诺中没有length
属性。而是使用count()
:
employees = elementController.getAllElements('css','#employee-list li');
employees.count().then(function (numberOfEmployees) {
var numRand = this.getRandomNumber(numberOfEmployees);
console.log(numRand);
elementController.doubleClick(employees.get(numRand));
});
也可以看看:
count() vs length in Protractor