单元测试 – “元素(选择器,标签).query(fn)”方法如何在Angular E2E测试中起作用?

前端之家收集整理的这篇文章主要介绍了单元测试 – “元素(选择器,标签).query(fn)”方法如何在Angular E2E测试中起作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在为Angular Scenario Runner编写E2E测试时,我遇到了一个query()方法

element(selector,label).query(fn)

documentation说:

Executes the function fn(selectedElements,done),where selectedElements are the elements that match the given jQuery selector and done is a function that is called at the end of the fn function. The label is used for test output.

所以我在html页面上为一组按钮写了一个it-case:

it('should display all buttons on page load',function () {
    var buttonText = ['1','2','3','4','5','6','7','8','9','0'];
    element('button','UI elements').query(function (selectedElements,done) {
        selectedElements.each(function (idx,elm) {
            expect(elm.innerText in buttonText).toEqual(true);
        });
        done();
    });
});

作为测试执行的结果,我收到了10个带有文本的失败expect-clauses的列表:

expected true but was undefined

中间调试显示buttonText条件下的elm.innerText是真实的.

所以我的问题是,出了什么问题?是不正确的使用done()方法

解决方法

你不能在element().query()函数调用expect().

但是你可以做到以下几点

var promise = element('SELECTOR').query(function(selectedElements,done) {
    // We are in JQuery land.
    if (selectedElements < 1) {
      done('ERROR MESSAGE')
    } else {
      done(null,selectedElements[0].innerText)
    }
    // For documentation only.
    // Don't return anything... just continue.
    done()
})
expect(promise).toEqual('i am waiting for you')

您可以将承诺和期望结合到一个电话中.

猜你在找的Angularjs相关文章