首页 > 解决方案 > Nightwatch JS 命令链在异步函数中不起作用

问题描述

我试图在我的 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

标签: javascriptnightwatch.jsnightwatch

解决方案


As much as it looks appealing to use chaining commands I suggest use this.api or browser.

It sounds like a chore but in the long run you will encounter less issues with methods and custom commands.

For example I can't use chaining commands if I need to use the expect by Mocha so I just rather use browser


推荐阅读