量角器:无法使用量角器获得所有元素

时间:2016-01-25 13:57:06

标签: javascript selenium protractor

我无法使用css或repeater获取所有元素。

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;
    };

从测试脚本调用上面的代码来查找所有元素,但它返回undefined。请建议!!

2 个答案:

答案 0 :(得分:1)

为什么不摆脱getAllElements函数,只使用简单的行:

employees = element.all(by.css('#employee-list li'))

employees = element.all(by.repeater(value))

完成后,您应该使用 then 语句确保在继续之前返回转发器的值。

employees = element.all(by.css('.items li')).then(function(returnedList) {
    numberOfEmployees = returnedList.length;
...
})

答案 1 :(得分:0)

getAllElements 返回一个将解析为元素数组的promise 。承诺上没有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));
});

另见: