量角器如何测试SELECT2 [英] Protractor how to test select2

查看:177
本文介绍了量角器如何测试SELECT2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择2下拉,你需要先输入两个字符,然后选择您的项目。我无法用量角器来测试这一点。

I have a select2 drop-down where you need to first enter 2 characters and then select your item. I am unable to test this with Protractor.

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

下面得到一个错误有关无法当您尝试的SendKeys关注的元素。

The following gets an error about being unable to focus the element when you try to sendKeys.

推荐答案

下面的代码片段成功激活并选择在选择2插件的第一个选项,它允许在网络上加载选项。

The following snippet successfully activates and selects the first option in a select2 widget, it allows for loading options over a network.

有几个问题与选择2部件 - 对于量角器E2E测试 - 这这个片段的地址。注释解释这一切非常好。

There are several issues with the 'select2' widget - in regard to protractor E2E testing - which this snippet addresses. The comments explains it all 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 provided scenario, you would use it like this:

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

这篇关于量角器如何测试SELECT2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆