2016-01-03 30 views
3

我想用量角器的by.repeater來查找集合中枚舉的所有元素。這裏使用的是ngRepeat指令的(key, value) in expression枚舉和UI Bootstrap accordion directive量角器by.repeater和visibilityOf只返回集合中的第一項

的HTML是:

<accordion id="automobile-types" close-others="true"> 
    <accordion-group heading="{{ autoType }}" 
    ng-repeat="(autoType, details) in automobiles"> 
    <div>Color of automobile:</div> 
     <ul> 
     <li ng-repeat="color in details.color"> 
      {{ color }} 
     </li> 
     </ul> 
    </accordion-group> 
</accordion> 

automobiles = { 
'Car': {'color': 'black', 'name': 'Knight Rider' }, 
'Truck': {'color': 'green', 'name': 'Biggins'} 
} 

使用protractor example in the angular docs for ngRepeat,這裏是我的量角器代碼:

var EC = protractor.ExpectedConditions; 
var cars = element.all(by.repeater('(autoType, details) in automobiles')); 

it('should have two automobiles listed', function() { 
    var visibleList = EC.visibilityOf(cars); 
    browser.wait(visibleList, 5000); 
    expect(cars.count()).toEqual(2); 
}); 

返回此故障:

Failed: Cannot call method 'bind' of undefined 

如果我刪除.all,測試將繼續進行,因爲它只找到ng重複中的第一個元素。這些測試都通過了:

var car = element(by.repeater('(autoType, details) in automobiles')); 
expect(car.getText()).toEqual('Car'); 

var accordion = element(element(by.id('automobile-types'))); 
expect(accordion.getText()).toEqual('Car\nTruck'); 

我嘗試用NG-重複啓動和NG-重複的高端解決這個問題,但沒有解決它,它仍然只返回集合中的第一個元素。

任何有關如何返回每個汽車的html塊的建議,將不勝感激。

回答

1

首先,你應該撥打element.all()裏面的it()。並且在您定義cars的行末尾有一個逗號。修正版本:

it('should have two automobiles listed', function() { 
    var cars = element.all(by.repeater('(autoType, details) in automobiles')); 
    expect(cars.count()).toEqual(2); 
}); 

如果仍然不能正常工作,請嘗試使用by.exactRepeater()

var cars = element.all(by.exactRepeater('(autoType, details) in automobiles')); 

可以切換到備用位置的技術:

var cars = $$('[ng-repeat="(autoType, details) in automobiles"]'); 

這裏,$$是一個快捷方式element.all(by.css())

+0

感謝alecxe的幫助和語法建議。我實現了關於使用'$$'的建議,我沒有想到這一點,但我仍然提出了同樣的失敗。我遇到的問題是元素選擇器不會返回列表的「.all」。它只返回重複集合中的第一個項目,同時仍然呈現HTML中的每個項目。我在想也許這是與bootstrap的accordion衍生物或ngRepeat的'key,pair'語法的衝突,但我無法找到有關這兩件事情的信息。 – JessG

+0

@JessG是的,好點,我想我以前多次看到這個錯誤,我懷疑這是一種通用錯誤。讓我們調試它。你可以發佈你的配置,完整的測試和追溯?謝謝,我們會解決它。 – alecxe

+0

@JessG也許嘗試打開''''''之前得到所有的元素。 。'''$( '#汽車類型')點擊(); ';''''''''''''''''''''我在那裏得到了完全相同的錯誤輸出,我記得我做了一些這樣的調整條件,以確保在JS完成所有正在進行的任務之前,Protractor'''element.all()'''不會被觸發。 –

1

看起來像關於Cannot call method 'bind' of undefined的失敗是對Expected Condition visibiltyOf的迴應,而不是像最初假設的by.repeater

看起來像visibiltyOf方法只傳遞單個元素,而元素列表正在傳遞。這些測試現在通過:

var EC = protractor.ExpectedConditions; 
var cars = element.all(by.repeater('(autoType, details) in automobiles')); 
var carTypes = element(by.id('automobile-types)); 

it('should have two automobiles listed', function() { 
    var visibleList = EC.visibilityOf(carTypes); 
    browser.wait(visibleList, 5000); 
    expect(cars.count()).toEqual(2); 
}); 
相關問題