6

我正在尝试使用量角器测试 ui-select。在这个 ui-select 中,我有一个国家列表。我的 html 看起来像:

<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-model 没有填充发送的数据。你有什么建议吗?谢谢

4

5 回答 5

7

我在这里找到了解决方案

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');
于 2015-06-26T10:08:15.627 回答
2

您还可以使用以下函数包装 ui-select:

    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-select 输入:

var input = new UiSelect(element(by.model('datiAnagrafici.countryOfBirth')));
input.sendKeys('IT'); 
input.pickChoice(0); // Pick choice with index 0 when searching for 'IT'. 
于 2016-03-14T23:08:58.617 回答
0

在发送密钥之前,单击ui-select元素

this.country.click();
this.country.sendKeys('IT');
于 2015-06-25T16:31:36.863 回答
0

单击 ui-select 元素并填充输入后,您应该通过添加以下内容来选择结果:

element.all(by.css('.ui-select-choices-row-inner span')).first().click();
于 2016-10-07T11:50:40.233 回答
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');;
于 2015-09-29T10:45:12.817 回答