【问题标题】:How do I assert the value of an input field with chai/chai-smoothie?如何使用 chai/chai-smoothie 断言输入字段的值?
【发布时间】:2018-11-08 08:43:54
【问题描述】:

感谢奶昔!

如何使用 chai/chai-smoothie 断言输入字段的值?

鉴于 getText() 始终为空,我们应该使用 element.getAttribute('value')(参见:How to getText on an input in protractor)

我希望能够做类似的事情:

expect(this.nameTextbox).to.eventually.have.value('name');

这似乎不起作用:

expect(this.nameTextbox.getAttribute('value')).to.eventually.equal('name');

AssertionError: expected { Object (browser_, then, ...) } to equal 'name'

【问题讨论】:

    标签: protractor chai cucumberjs serenity-js


    【解决方案1】:

    chai-smoothie 是在断言失败时启用错误消息更易于阅读。但它无法处理承诺:this.nameTextbox.getAttribute('value') 返回一个承诺。

    注意:所有 Protractor API 都返回承诺。

    你需要使用chai-as-promised和chai来处理promise。

    var chai = require('chai'),
    
    chai.use(require('chai-as-promised'));
    chai.use(require('chai-smoothie'));
    
    global.expect = chai.expect;
    
    // then can do assertion as following:
    expect(this.nameTextbox.getAttribute('value')).to.eventually.equal('name');
    

    【讨论】:

    • 我可以同时使用 chai-as-promised 和 chai-smoothie 吗?我将如何配置它?
    • 是的,请参阅我更新的示例代码。但要注意这3个包的兼容版本。
    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2012-07-16
    • 1970-01-01
    相关资源
    最近更新 更多