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

时间:2019-01-23 17:48:32

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

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

具有两个'.expect'断言使测试失败并显示错误消息 .expept 当我仅使用一个命令运行它时,运行正常。

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();
   },
};

1 个答案:

答案 0 :(得分: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;
    }
}]