6

I am trying to test an ui-select with protractor. In this ui-select I have a list of countries. My html looks like:

<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>

My page object looks like:

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

In my spec file I have:

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

But when i run my test the ng-model is not filled with the sent data. Do you have any tips? Thanks

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Gianfra
  • 1,119
  • 4
  • 17
  • 33

5 Answers5

7

I have found the solution here

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

then in the fill form method is almost as alecxe said:

this.country.click();
this.selectCountry.sendKeys('Italy');
Gianfra
  • 1,119
  • 4
  • 17
  • 33
3

You can also wrap an ui-select with a function like that:

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

Now it's easier to manipulate any ui-select input:

var input = new UiSelect(element(by.model('datiAnagrafici.countryOfBirth')));
input.sendKeys('IT'); 
input.pickChoice(0); // Pick choice with index 0 when searching for 'IT'. 
Stéphane
  • 1,528
  • 14
  • 21
0

Before sending keys, click the ui-select element:

this.country.click();
this.country.sendKeys('IT');
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • It opens the ui-select but it does not write the key. Its like it loses the focus on the ui-select. – Gianfra Jun 26 '15 at 07:35
0

While I couldn't get sniper87's answer to work for me, by playing around with the css this worked correctly:

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');;
Niel
  • 1,856
  • 2
  • 23
  • 45
  • It is right as well, I have used the reference by model and you have used the reference by css, both right approach. – Gianfra Oct 04 '15 at 16:55
0

after clicking the ui-select element and filling the input, you should then select result by adding this :

element.all(by.css('.ui-select-choices-row-inner span')).first().click();
Massi Issar
  • 403
  • 5
  • 23