2015-06-25 16 views
5

我正在嘗試使用量角器測試ui-select。在這個ui-選擇我有一個國家的名單。 我的HTML看起來像:使用ui-select使用量角器測試

<ui-select ng-model="datiAnagrafici.countryOfBirth" name="countryOfBirth" theme="bootstrap" reset-search-input="true" append-to-body="true" required blur> 
       <ui-select-match placeholder="paese di nascita">{{$select.selected.name}}</ui-select-match> 
       <ui-select-choices repeat="country.code as country in countries | filter: {name:$select.search}"> 
        <span ng-bind-html="country.name"></span> 
       </ui-select-choices> 
</ui-select> 

我的頁面對象的樣子:

this.country = element(by.model('datiAnagrafici.countryOfBirth')); 
this.fillForm = function(){ 
    this.country.sendKeys('IT'); 
} 

在我的規格文件我有:

it('should fill the form', function() { 
    form.fillForm(); 
}) 

但是當我運行我的測試中NG-模型沒有填寫發送的數據。 你有任何提示嗎? 感謝

回答

5

我已經找到了解決辦法here

this.country = element(by.model('datiAnagrafici.countryOfBirth')); 
this.selectCountry = this.country.element(by.css('.ui-select-search')); 

然後在填表方法幾乎一樣alecxe說:

this.country.click(); 
this.selectCountry.sendKeys('Italy'); 
+0

你應該將此標記爲答案:) –

+0

這是否適用於phantomjs? – rave

0

發送密鑰之前,點擊ui-select元素

this.country.click(); 
this.country.sendKeys('IT'); 
+0

它打開ui-select,但它不寫入密鑰。它就像失去了對UI選擇的關注。 – Gianfra

0

雖然我不能讓sniper87的回答爲我工作,通過與CSS玩弄這工作正常:

element(by.css('div.ui-select-container div.ui-select-match span.ui-select-input')).click(); 
element(by.css('div.ui-select-container .ui-select-search')).sendKeys('foo');; 
+0

這也是對的,我已經使用了模型的參考,並且您使用了css的引用,都是正確的方法。 – Gianfra

1

你也可以換一個用戶界面,選擇具有這樣的功能:

function UiSelect(elem) { 
     var self = this; 

     self._input = elem; 
     self._selectInput = self._input.element(by.css('.ui-select-search')); 
     self._choices = self._input.all(by.css('.ui-select-choices .ui-select-choices-row-inner')); 

     self.sendKeys = function(val) { 
      self._input.click(); 
      self._selectInput.clear(); 
      return self._selectInput.sendKeys(val); 
     }; 

     self.pickChoice = function(index){ 
      browser.waitForAngular(); 
      expect(self._choices.count()).not.toBeLessThan(index + 1); 
      return self._choices.get(index).click(); 
     }; 
    }; 

現在,它更容易操控所有UI選擇輸入:

var input = new UiSelect(element(by.model('datiAnagrafici.countryOfBirth'))); 
input.sendKeys('IT'); 
input.pickChoice(0); // Pick choice with index 0 when searching for 'IT'. 
0

點擊UI選元素和填充輸入後,通過添加這應該然後選擇結果:

element.all(by.css('.ui-select-choices-row-inner span')).first().click(); 
相關問題