Nightwatch: ES6 async/awaits browser.elements() resolves Nightwatch API

Created on 10 Mar 2020  路  6Comments  路  Source: nightwatchjs/nightwatch

Describe the bug

Async/awaits with browser.elements function returns Nightwatch API instead of array of web elements.

Sample test

sampleTest.js


const EventEmitter = require('events')

class sampleTest extends EventEmitter {
  async command () {
    const browser = this.api
    const result = await browser.elements('css selector', '*[data-id="selector"]')

    console.log('result: ', result) //logs Nightwatch API to console
    // result.value is undefined
    result.value.forEach(async function (jsonWebElement) {
      const jsonWebElementId = jsonWebElement.ELEMENT
    })
    this.emit('complete')
    return this
  }
}

module.exports = sampleTest

Run with command

$ nightwatch test/sampleTest.js --your-other-arguments-here

Verbose output

debug.log


Running: sampleTest

Result: TypeError: Error while running "sampleTest" command: Cannot read property 'forEach' of undefined

Configuration

nightwatch.json


{
  "your": { "config": "here" }
}

Your Environment


| nightwatch --version | 1.3.4 |
| npm --version | 6.9.0 |
| node --version | 10.16.2 |
| selenium-server | 3.141 |
| Ubuntu | 18.04 |
-->

bug stale

All 6 comments

It should be said that this only happens when writing custom commands.

The same also applies to browser.getText() when writing custom commands.

I'm facing the same issue when using const result = await this.api.getLocationInView('xpath', selector); inside a custom assertion.

Has anyone had any luck finding a workaround for this?

Depending on context, I am also also encountering cases where the whole call becomes async and returns, despite the use of await.

Using the command above, and something like:

const res = await browser.command()
browser.perform(() => { console.log('after command') })

I get output:

after command
[...]
result

This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

+1

Same if I do the below, copied from https://nightwatchjs.org/api/getWindowSize.html

const value = await browser.getWindowSize();
console.log('value', value);

Was this page helpful?
0 / 5 - 0 ratings