gpt4 book ai didi

使用 Angular-UI Select2 元素进行 Angularjs E2E 测试

转载 作者:行者123 更新时间:2023-12-04 14:14:11 24 4
gpt4 key购买 nike

我有一个使用 Angular UI 的带有 select2 元素的部分 http://angular-ui.github.io/

我遇到的问题是该元素是必需的,尽管我已通过以下代码成功设置了该字段,但未删除 required 属性,因为 Angular 的模型不得因外部更改而更新,我不知道如何要么提供 $scope.apply() 要么利用 Angular 的另一个函数来继续测试。

首先允许直接运行 jQuery 函数:(取自 How to execute jQuery from Angular e2e test scope?)

angular.scenario.dsl('jQueryFunction', function() {
return function(selector, functionName /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
return this.addFutureAction(functionName, function($window, $document, done) {
var $ = $window.$; // jQuery inside the iframe
var elem = $(selector);
if (!elem.length) {
return done('Selector ' + selector + ' did not match any elements.');
}
done(null, elem[functionName].apply(elem, args));
});
};
});

然后更改字段值:
jQueryFunction('#s2id_autogen1', 'select2', 'open');
jQueryFunction('#s2id_autogen1', 'select2', "val", "US");
jQueryFunction('#s2id_autogen1', 'select2', 'data', {id: "US", text: "United States"});
jQueryFunction('.select2-results li:eq(3)', 'click');
jQueryFunction('#s2id_autogen1', 'trigger', 'change');
jQueryFunction('#s2id_autogen1', 'select2', 'close');
input('request._countrySelection').enter('US');

请注意,并非所有这些功能都需要反射(reflect)对 ui 的更改,只是我用来尝试使其工作的所有功能......

最佳答案

为了让它发挥作用,我咨询了布赖恩的回答和正弦律,但在我的情况下它仍然失败,原因有两个:

  • 单击“div#s2id_autogen1”不会为我打开 select2 输入,我使用的选择器是“div#s2id_autogen1 a”
  • 获取 select2 元素我会得到 ElementNotVisibleError,可能是因为我的 select2 在引导模式中,所以我在单击它之前明确等待元素可见(您可以阅读我阅读的原始提示以使用此 here )。

  • 结果代码是:
    function select2ClickFirstItem(select2Id) {
    var select2Input;

    // Wait for select2 element to be visible
    browser.driver.wait(function() {
    select2Input = element(by.css('#s2id_' + select2Id + ' a'));
    return select2Input;
    }).then(function() {
    select2Input.click();

    var items = element.all(by.css('.select2-results-dept-0'));
    browser.driver.wait(function () {
    return items.count().then(function (count) {
    return 0 < count;
    });
    });
    items.get(0).click();
    });
    }

    希望能帮助到你。

    关于使用 Angular-UI Select2 元素进行 Angularjs E2E 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340684/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com