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

5 Answers 5

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');
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Before sending keys, click the ui-select element:

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

1 Comment

It opens the ui-select but it does not write the key. Its like it loses the focus on the ui-select.
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');;

1 Comment

It is right as well, I have used the reference by model and you have used the reference by css, both right approach.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.