如何使用页面对象 运行 Nightwatch.js 中的多个 chai 断言?

How to run multiple chai assertion in Nightwatch.js using page object?

在我的测试中,我需要验证多个页面上的相同文本行。 我正在尝试使用 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')
        }
    }]
}

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

Chai's assertions, Nightwatch's built-in assertions, or mostly any other assertions library, work the same way! Assertions are breaking statements (meaning your program should end execution on one's failure/exception thrown), with a clear scope & purpose: evaluating a predicate. Two assertions will always be independent of one another. Thus, there's no logical case concern for chaining two, or more assertions, now is there?

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

所以,你不能这样做...

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;
    }
}]