【问题标题】:How to run multiple chai assertion in Nightwatch.js using page object?如何使用页面对象在 Nightwatch.js 中运行多个 chai 断言?
【发布时间】:2019-06-17 09:06:01
【问题描述】:

在我的测试中,我需要在多个页面上验证相同的文本行。 我正在尝试使用 chai 断言,但无法使用页面对象运行多个 .expept 断言。

有两个 '.expect' 断言使测试失败并显示错误消息 Unknown property: "expect". Please consult docs at:http://nightwatchjs.org/api. 当我只用一个命令运行它时,它运行正常。

// Test file code

module.exports = {

    'Copy Test': client => {
    client.url('https://www.testsite.com/')
    client.page.search().checkText()
    client.end();
   },
};
// Page object file code

let copyP = "Some test text"
let copyP2 = "Some text text 2"

module.exports = {
    elements: {
        p: 'CSS_selector',
        p2: 'CSS_selector',
    },

    commands: [{
        checkText: function() {
            return this 
            .expect.element('@p').text.to.equal( copyP, 'Text is ok')
            .expect.element('@p2').text.to.equal( copyP2, 'Text2 is ok')
        }
    }]
}

【问题讨论】:

    标签: automation ui-automation nightwatch.js assertion pageobjects


    【解决方案1】:

    是的,您详细说明的是正确和期望的行为

    Chai 的断言,Nightwatch 的内置断言,或者大多数 其他断言库,工作方式相同!断言是破坏性的 语句意味着你的程序应该结束执行 失败/抛出异常),具有明确的范围和目的:评估 一种 predicate。 两个断言总是相互独立的。因此, 链接两个或多个断言没有逻辑上的案例关注, 现在有吗?

    基本上,断言不支持回调函数,因此您不能将一个结果传递给另一个(它们没有内置逻辑来执行此操作)。

    所以,你不能这样做……

    browser.click('@someElem')
           .expect.element('@otherElem').to.be.visible
           .expect.element('@otherElem').text.to.equal('I<3Turtles', 'text check');
    

    你不能这样做……

    browser.click('@someElem')
           .expect.element('@otherElem').to.be.visible
           .setValue('@otherElem', 'I like turtles');
    

    ...既然我们已经解决了这些问题,让我们看看如何重构该命令:

    commands: [{
        checkText: function() {
            // Perform wrapper for extra safety! 
            this.api.perform((done) => {
                this.expect.element('@p').text.to.equal( copyP, 'Text is ok');
                this.expect.element('@p2').text.to.equal( copyP2, 'Text2 is ok');
    
                done();
            });
            return this;
        }
    }]
    

    【讨论】:

    • 这行得通,感谢@iamdanchiv 的帮助。假设每个人一次只需要运行一个断言是很奇怪的。在我看来,最好在配置文件中的某个地方使断言可破坏或不可用。但应该有充分的理由说明它的工作方式。
    • 嗯,是的,Nightwatch 支持类似的东西。如果您不想要经典的fail-fast 类型的断言,那么您可以查看verify statements here。这些在测试结束时作为集合失败,不会破坏程序和运行时。希望能帮助到你。干杯!
    • 是的,我实际上使用了这些断言,特别是.containsText()但我发现了一个错误,如果您检查的副本缺少第一个字母,它不会失败。
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多