Protractor how to check select2

I have a select2 dropdown where you need to enter 2 characters first and then select your item. I cannot verify this with Protractor.

var select2 = element(by.css('div#s2id_person'));
select2.click();
select2.sendKeys('ip');
select2.sendKeys(protractor.Key.ENTER);

      

Below is the error that you cannot focus the element when trying to sendKeys.

+2


source to share


1 answer


The following snippet successfully activates and selects the first option in the select2 widget, it allows the parameters to be loaded over the network.

There are several problems with the "select2" widgets - in relation to testing the E2E prototype this snippet refers to. The comments explain this very well.

/**
 * @param {string} select2Locator // CSS selector of select2 container
 * @param {string} opt_query // an optional Query string
 */
function select2First(select2Locator, opt_query){
    // the 'a' element inside the select2 will receive the 'mousedown' event
    var selector = select2Locator + ' a.select2-choice';
    // Locator for the select2 options
    var options = element.all(by.css('.select2-results-dept-0'));

    // select2 doesn't activate on click 
    // and protractor doesn't have a direct mousedown method on 'ElementFinder'.
    browser.driver.executeScript('$(arguments["0"]).mousedown();', (selector));

    if(opt_query){
        browser.driver.switchTo().activeElement().sendKeys(opt_query);
        // select2 can fetch options from over a network
        // so we confirm that all pending network requests are resolved after typing the query
        browser.driver.wait(function(){
            return browser.driver.executeScript('return $.active === 0;');
        }, 2000);
    }

    // makes sure all the options are rendered
    browser.driver.wait(function(){
        return options.count().then(function(count){
            return 0 < count;
        });
    }, 2000);

    options.first().click();
};

      



In your suggested scenario, you would use it like this:

select2First('div#s2id_person', 'ip');

      

+8


source







All Articles