【问题标题】:Nightwatch JS command chaining doesn't work in async functionsNightwatch JS 命令链在异步函数中不起作用
【发布时间】:2021-03-07 17:51:32
【问题描述】:

我试图在我的 Nightwatch 测试中调用异步页面对象命令,但测试失败。正如您从代码示例中看到的那样,我试图将几个 .pause 命令链接在一起,但链接机制不起作用。如果我删除“异步”关键字并注释掉“等待”代码,则链接命令将起作用。因此,似乎使用“异步”会破坏命令链接。有解决办法吗?

守夜测试 ...

module.exports = {
    'Test Commmand Queue': async function (browser) {
        browser.page.clients.sandbox.simpleTestPageObject().testCommandQueue() // async function
    }
}

页面对象文件...

module.exports = {
    elements: {},
    commands: [{
        testCommandQueue: async function () {
            this
                .pause(1)
                .pause(1)

            console.log("0001")
            await this.pause(3000, function () {
                console.log("0002")
            })
            console.log("0003")
        }
    }]
}

输出...

Running:  Test Commmand Queue
_________________________________________________

TEST FAILURE: 1 error during execution; 0 tests failed, 0 passed (4.939s)

  TypeError: this.pause(...).pause is not a function

守夜人 v 1.5.0

【问题讨论】:

    标签: javascript nightwatch.js nightwatch


    【解决方案1】:

    尽管使用链接命令看起来很有吸引力,但我建议使用 this.apibrowser

    这听起来像是一件苦差事,但从长远来看,您在方法和自定义命令方面遇到的问题会更少。

    例如,如果我需要使用expect by Mocha,我不能使用链接命令,所以我宁愿使用browser

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2022-11-03
      • 2021-09-05
      • 2021-07-02
      相关资源
      最近更新 更多