Nightwatch JS command chaining doesn't work in async functions

395 views Asked by At

I'm trying to call an async page object command in my Nightwatch test but the test is failing. 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. 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 ...

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 ...

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

1

There are 1 answers

0
Vladislav Bulanov On

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