Testing with protractor using ui-select
Asked Answered
B

5

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

Blackmon answered 25/6, 2015 at 16:4 Comment(0)
B
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');
Blackmon answered 26/6, 2015 at 10:8 Comment(0)
D
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'. 
Donahue answered 14/3, 2016 at 23:8 Comment(0)
D
0

Before sending keys, click the ui-select element:

this.country.click();
this.country.sendKeys('IT');
Defence answered 25/6, 2015 at 16:31 Comment(1)
It opens the ui-select but it does not write the key. Its like it loses the focus on the ui-select.Blackmon
R
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');;
Roentgenograph answered 29/9, 2015 at 10:45 Comment(1)
It is right as well, I have used the reference by model and you have used the reference by css, both right approach.Blackmon
M
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();
Metonymy answered 7/10, 2016 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.