I'm trying to start with Nightwatch and faced the unclear behavior.
Please take a look at the the following code. I want to gather some text from a page into the list and then print it. Note the async
test and await browser.perform
call:
module.exports = {
'@tags': ['smoke test'],
'Work with a table': async browser => {
browser.page.mainPage.tablePage().navigate();
let list = []
await browser
.perform(function () {
browser.elements('css selector', 'thead th', function (elements) {
elements.value.forEach(function (elementsObj, index) {
browser.elementIdText(elementsObj.ELEMENT, function (result) {
list.push(result.value)
console.log('>> ' + result.value);
})
})
})
})
console.log('list: ' + list);
console.log('end');
}
}
In my understanding, I should see the result of console.log('list: ' + list)
as a fulfilled list at the end of the test.
But the output is:
\ Running Work with a table:
list:
| Running Work with a table:
>> Name
>> Position
>> Office
>> Age
>> Start date
‼ Running Work with a table:
No assertions ran.
So the console.log('list: ' + list);
was executed before. What I'm doing wrong here?
Another one question is why the last console.log('end');
is not printed at all?
Update
I tried to name the test as 'Work with a table': async function(browser)
, with the same result. And if I add the await
keyword to the browser.page.mainPage.tablePage().navigate();
command, there is no console log printed at all.
Update2
The result of broser.perform
is a Promise,
When I print it, I get the following:
Promise {
<pending>,
capabilities: {
acceptInsecureCerts: false,
acceptSslCerts: false,
applicationCacheEnabled: false,
browserConnectionEnabled: false,
. . .
browser.perform
, but I guess it does not return a Promise (you could try that out, by console.log-ing thebrowser.perform
return value). If it's not a promise, thenawait
simply resolves the returned value and doesn't wait for anythingperform
is asynchronous, soperform
thinks it is finished even though some asynchronous code is not yet finished. If that's the case you likely need thedone
callback described in the API reference and call it when everything inside is finished. I don't have enough Nightwatch experience to help you with this, though