2016-01-25 84 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; 
... 
})