加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:12:15 所属栏目:安全 来源:网络整理
导读:在为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 sele
在为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')

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读