简体   繁体   English

Nightwatch JS 命令链在异步函数中不起作用

[英]Nightwatch JS command chaining doesn't work in async functions

I'm trying to call an async page object command in my Nightwatch test but the test is failing.我试图在我的 Nightwatch 测试中调用异步页面 object 命令,但测试失败。 As you can see by the code sample, I'm attempting to chain a couple of.pause commands together but the chaining mechanism is not working.正如您在代码示例中看到的那样,我试图将几个 .pause 命令链接在一起,但链接机制不起作用。 If I remove the 'async' keywords and comment out the 'await' code then the chained commands work.如果我删除“异步”关键字并注释掉“等待”代码,则链接命令将起作用。 So it seems that using 'async' breaks command chaining.因此,似乎使用“异步”会破坏命令链接。 Is there a solution for this?有解决方案吗?

Nightwatch test...夜班测试...

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

page object file...页 object 文件...

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")
        }
    }]
}

output... output...

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

Nightwatch v 1.5.0守夜人 v 1.5.0

As much as it looks appealing to use chaining commands I suggest use this.api or browser .尽管使用链接命令看起来很有吸引力,但我建议使用this.apibrowser

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例如,如果我需要使用Mochaexpect ,我不能使用链接命令,所以我宁愿使用browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM