2016-01-25 31 views
2

我无法使用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; 
    }; 

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

回答

0

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

参见:

1

你为什么不摆脱getAllElements功能,只需使用简单的线条:

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

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

你已经这样做了之后,你应该那么很可能使用然后声明以确保您在继续之前返回中继器的值。

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