nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.85k stars 1.34k forks source link

Second call of the `elementIdElements()` returns the NightwatchAPI object instead of the expected result #4273

Open MurzNN opened 1 month ago

MurzNN commented 1 month ago

Description of the bug/issue

When I call the this.api.elementIdElements() in a custom command, called from the sync function, on the second call it returns the NightwatchAPI object instead of the actual result.

So, the first call of result = this.api.elementIdElements() returns the array of elements, but when doing the same call a second time - it suddenly returns the NigthwatchAPI object instead!

If I do the same from the async function - all work well.

See the test code below on how to reproduce.

Steps to reproduce

  1. Create a custom Nigthwatch command that calls the function this.api.elementIdElements() multiple times.
  2. Call this command from a Nightwatch test from an async function - all will work well.
  3. Call this command from a Nightwatch test from a sync function - the second call will return the NigthwatchAPI object instead of the expected result.

Sample test

// A test file to call the custom command: Nightwatch/Tests/test1.js

module.exports = {
  'Test thFindNested in async mode': async (browser) => {
    await browser.url('http://google.com');
    const result = await browser.thFindNested();
    console.log('result', result);
  },

  'Test thFindNested in sync mode': (browser) => {
    browser
      .url('http://google.com')
      .thFindNested(result => {
        console.log('result', result);
      });
  },
}

// A custom command file: Nightwatch/Commands/thFindNested.js

module.exports = class thFindNested {
  async command(callback) {
    const el1 = await this.api.findElement('body');
    console.log('el1', el1);
    const el1Id = el1.getId();
    console.log('el1Id', el1Id);

    const els2 = await this.api.elementIdElements(el1Id, 'css selector', 'div')
    console.log('els2', els2);
    const el2 = els2[0];
    console.log('el2', el2);
    // The function elementIdElements() returns elements without the getId() method.
    const el2Id = Object.values(el2)[0];
    console.log('el2Id', el2Id);

    const els3 = await this.api.elementIdElements(el2Id, 'css selector', 'div')
    console.log('els3', els3);
    const el3 = els3[0];
    console.log('el3', el3);
    // The function elementIdElements() returns elements without the getId() method.
    const el3Id = Object.values(el3)[0];
    console.log('el3Id', el3Id);

    if (typeof callback === 'function') {
      const self = this;
      const result = {
        status: 0,
        value: el3Id
      }
      callback.call(self, result);
    }

  }

};

Command to run

nightwatch Tests/test1.js

Verbose Output

[Tests/Test1] Test Suite
────────────────────────────────────────────────
ℹ Connected to selenium-chrome on port 4444 (354ms).
  Using: chrome (101.0.4951.41) on LINUX.

  Running Test thFindNested in async mode:
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  ℹ Loaded url http://google.com in 2566ms
el1 {
  'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-1',
  getId: [Function (anonymous)]
}
el1Id 0.9139286585070521-1
els2 [
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-2' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-3' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-4' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-5' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-6' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-7' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-8' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-9' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-10' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-11' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-12' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-13' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-14' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-15' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-16' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-17' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-18' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-19' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-20' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-21' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-22' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-23' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-24' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-25' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-26' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-27' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-28' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-29' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-30' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-31' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-32' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-33' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-34' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-35' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-36' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-37' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-38' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-39' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-40' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-41' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-42' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-43' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-44' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-45' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-46' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-47' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-48' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-49' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-50' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-51' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-52' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-53' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-54' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-55' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-56' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-57' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-58' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-59' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-60' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-61' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-62' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-63' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-64' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-65' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-66' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-67' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-68' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-69' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-70' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-71' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-72' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-73' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-74' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-75' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-76' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-77' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-78' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-79' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-80' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-81' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-82' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-83' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-84' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-85' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-86' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-87' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-88' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-89' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-90' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-91' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-92' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-93' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-94' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-95' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-96' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-97' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-98' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-99' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-100' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-101' },
  ... 56 more items
]
el2 { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-2' }
el2Id 0.9139286585070521-2
els3 [
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-3' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-4' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-5' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-6' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-7' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-8' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-9' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-10' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-11' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-12' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-13' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-14' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-15' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-16' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-17' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-18' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-19' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-20' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-21' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-22' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-23' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-24' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-25' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-26' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-27' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-28' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-29' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-30' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-31' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-32' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-33' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-34' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-35' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-36' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-37' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-38' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-39' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-40' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-41' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-42' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-43' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-44' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-45' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-46' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-47' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-48' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-49' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-50' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-51' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-52' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-53' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-54' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-55' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-56' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-57' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-58' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-59' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-60' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-61' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-62' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-63' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-64' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-65' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-66' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-67' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-68' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-69' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-70' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-71' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-72' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-73' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-74' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-75' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-76' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-77' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-78' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-79' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-80' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-81' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-82' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-83' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-84' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-85' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-86' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-87' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-88' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-89' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-90' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-91' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-92' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-93' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-94' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-95' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-96' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-97' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-98' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-99' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-100' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-101' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-102' },
  ... 49 more items
]
el3 { 'element-6066-11e4-a52e-4f735466cecf': '0.9139286585070521-3' }
el3Id 0.9139286585070521-3
result 0.9139286585070521-3
No assertions ran.

  Running Test thFindNested in sync mode:
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  ℹ Loaded url http://google.com in 693ms
el1 {
  'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-1',
  getId: [Function (anonymous)]
}
el1Id 0.4891503280064242-1
els2 [
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-2' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-3' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-4' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-5' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-6' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-7' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-8' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-9' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-10' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-11' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-12' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-13' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-14' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-15' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-16' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-17' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-18' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-19' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-20' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-21' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-22' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-23' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-24' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-25' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-26' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-27' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-28' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-29' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-30' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-31' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-32' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-33' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-34' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-35' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-36' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-37' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-38' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-39' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-40' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-41' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-42' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-43' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-44' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-45' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-46' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-47' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-48' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-49' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-50' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-51' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-52' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-53' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-54' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-55' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-56' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-57' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-58' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-59' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-60' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-61' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-62' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-63' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-64' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-65' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-66' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-67' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-68' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-69' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-70' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-71' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-72' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-73' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-74' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-75' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-76' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-77' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-78' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-79' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-80' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-81' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-82' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-83' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-84' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-85' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-86' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-87' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-88' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-89' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-90' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-91' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-92' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-93' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-94' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-95' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-96' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-97' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-98' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-99' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-100' },
  { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-101' },
  ... 55 more items
]
el2 { 'element-6066-11e4-a52e-4f735466cecf': '0.4891503280064242-2' }
el2Id 0.4891503280064242-2
els3 NightwatchAPI {
  capabilities: {
    acceptInsecureCerts: false,
    acceptSslCerts: false,
    browserConnectionEnabled: false,
    browserName: 'chrome',
    chrome: {
      chromedriverVersion: '101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904})',
      userDataDir: '/tmp/.org.chromium.Chromium.QTCE5K'
    },
    cssSelectorsEnabled: true,
    databaseEnabled: false,
    'goog:chromeOptions': { debuggerAddress: 'localhost:42087' },
    handlesAlerts: true,
    hasTouchScreen: false,
    javascriptEnabled: true,
    locationContextEnabled: true,
    mobileEmulationEnabled: false,
    nativeEvents: true,
    networkConnectionEnabled: false,
    pageLoadStrategy: 'normal',
    platform: 'LINUX',
    proxy: {},
    rotatable: false,
    'se:cdp': 'ws://172.20.0.4:4444/session/2a0897323e3a02eb3f4b4677398fc05b/se/cdp',
    'se:cdpVersion': '101.0.4951.41',
    'se:vnc': 'ws://172.20.0.4:4444/session/2a0897323e3a02eb3f4b4677398fc05b/se/vnc',
    'se:vncEnabled': true,
    'se:vncLocalAddress': 'ws://172.20.0.4:7900',
    setWindowRect: true,
    strictFileInteractability: false,
    takesHeapSnapshot: true,
    takesScreenshot: true,
    timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
    unexpectedAlertBehaviour: 'ignore',
    unhandledPromptBehavior: 'ignore',
    version: '101.0.4951.41',
    webStorageEnabled: true,
    'webauthn:extension:credBlob': true,
    'webauthn:extension:largeBlob': true,
    'webauthn:virtualAuthenticators': true
  },
  currentTest: [Getter],
  ...
  ... [a lot of properties of the NightwatchAPI object]
  ...
  axeInjectFunc2: [Function: bound queuedCommandFn]
}
el3 undefined
  ✖ TypeError
   Cannot convert undefined or null to object
    Stack Trace :
    at Function.values (<anonymous>)
    at CommandInstance.command (/var/www/html/web/modules/contrib/test_helpers/tests/modules/test_helpers_functional/tests/src/Nightwatch/Commands/thFindNested.js:23:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

  ✖ TypeError
   Error while running "thFindNested" command: [TypeError] Cannot convert undefined or null to object
    Stack Trace :
    at Function.values (<anonymous>)
    at CommandInstance.command (/var/www/html/web/modules/contrib/test_helpers/tests/modules/test_helpers_functional/tests/src/Nightwatch/Commands/thFindNested.js:23:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

  FAILED: 1 assertions failed and  1 errors (994ms)

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  ️TEST FAILURE (4.942s): 
   - 1 error during execution; 
   - 1 assertions failed; 0 passed

   ✖ 1) Tests/test1

   – Test thFindNested in sync mode (994ms)

   →   TypeError   Error while running "thFindNested" command: [TypeError] Cannot convert undefined or null to object

  - OTHER ERRORS:
  Error
    ✖ TypeError
   Cannot convert undefined or null to object
    Stack Trace :
    at Function.values (<anonymous>)
    at CommandInstance.command (/var/www/html/web/modules/contrib/test_helpers/tests/modules/test_helpers_functional/tests/src/Nightwatch/Commands/thFindNested.js:23:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

 Wrote HTML report file to: /var/www/html/web/core/reports/nightwatch/nightwatch-html-report/index.html

error Command failed with exit code 5.

Nightwatch Configuration

module.exports = {
  // An array of folders (excluding subfolders) where your tests are located;
  // if this is not specified, the test source must be passed as the second argument to the test runner.
  src_folders: [],

  // See https://nightwatchjs.org/guide/concepts/page-object-model.html
  page_objects_path: ['node_modules/nightwatch/examples/pages/'],

  // See https://nightwatchjs.org/guide/extending-nightwatch/adding-custom-commands.html
  custom_commands_path: ['node_modules/nightwatch/examples/custom-commands/'],

  // See https://nightwatchjs.org/guide/extending-nightwatch/adding-custom-assertions.html
  custom_assertions_path: '',

  // See https://nightwatchjs.org/guide/extending-nightwatch/adding-plugins.html
  plugins: [],

  // See https://nightwatchjs.org/guide/concepts/test-globals.html#external-test-globals
  globals_path : '',

  webdriver: {},

  test_workers: {
    enabled: true,
    workers: 'auto'
  },

  test_settings: {
    default: {
      disable_error_log: false,
      launch_url: 'https://nightwatchjs.org',

      screenshots: {
        enabled: false,
        path: 'screens',
        on_failure: true
      },

      desiredCapabilities: {
        browserName : 'firefox'
      },

      webdriver: {
        start_process: true,
        server_path: ''
      }
    },

    firefox: {
      desiredCapabilities : {
        browserName : 'firefox',
        acceptInsecureCerts: true,
        'moz:firefoxOptions': {
          args: [
            // '-headless',
            // '-verbose'
          ]
        }
      },
      webdriver: {
        start_process: true,
        server_path: '',
        cli_args: [
          // very verbose geckodriver logs
          // '-vv'
        ]
      }
    },

    chrome: {
      desiredCapabilities : {
        browserName : 'chrome',
        'goog:chromeOptions' : {
          // More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
          //
          // w3c:false tells Chromedriver to run using the legacy JSONWire protocol (not required in Chrome 78)
          w3c: true,
          args: [
            //'--no-sandbox',
            //'--ignore-certificate-errors',
            //'--allow-insecure-localhost',
            //'--headless'
          ]
        }
      },

      webdriver: {
        start_process: true,
        server_path: '',
        cli_args: [
          // --verbose
        ]
      }
    },

    edge: {
      desiredCapabilities : {
        browserName : 'MicrosoftEdge',
        'ms:edgeOptions' : {
          w3c: true,
          // More info on EdgeDriver: https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options
          args: [
            //'--headless'
          ]
        }
      },

      webdriver: {
        start_process: true,
        // Download msedgedriver from https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/
        //  and set the location below:
        server_path: '',
        cli_args: [
          // --verbose
        ]
      }
    },

    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using cucumber-js (https://cucumber.io)                |
    //                                                                               |
    // It uses the bundled examples inside the nightwatch examples folder; feel free |
    // to adapt this to your own project needs                                       |
    //////////////////////////////////////////////////////////////////////////////////
    'cucumber-js': {
      src_folders: ['examples/cucumber-js/features/step_definitions'],

      test_runner: {
        // set cucumber as the runner
        type: 'cucumber',

        // define cucumber specific options
        options: {
          //set the feature path
          feature_path: 'node_modules/nightwatch/examples/cucumber-js/*/*.feature',

          // start the webdriver session automatically (enabled by default)
          // auto_start_session: true

          // use parallel execution in Cucumber
          // workers: 2 // set number of workers to use (can also be defined in the cli as --workers=2
        }
      }
    },

    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using the browserstack.com cloud service               |
    //                                                                               |
    // Please set the username and access key by setting the environment variables:  |
    // - BROWSERSTACK_USERNAME                                                       |
    // - BROWSERSTACK_ACCESS_KEY                                                     |
    // .env files are supported                                                      |
    //////////////////////////////////////////////////////////////////////////////////
    browserstack: {
      selenium: {
        host: 'hub.browserstack.com',
        port: 443
      },
      // More info on configuring capabilities can be found on:
      // https://www.browserstack.com/automate/capabilities?tag=selenium-4
      desiredCapabilities: {
        'bstack:options' : {
          userName: '${BROWSERSTACK_USERNAME}',
          accessKey: '${BROWSERSTACK_ACCESS_KEY}',
        }
      },

      disable_error_log: true,
      webdriver: {
        timeout_options: {
          timeout: 15000,
          retry_attempts: 3
        },
        keep_alive: true,
        start_process: false
      }
    },

    'browserstack.local': {
      extends: 'browserstack',
      desiredCapabilities: {
        'browserstack.local': true
      }
    },

    'browserstack.chrome': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions : {
          w3c: true
        }
      }
    },

    'browserstack.firefox': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'firefox'
      }
    },

    'browserstack.ie': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'internet explorer',
        browserVersion: '11.0'
      }
    },

    'browserstack.safari': {
      extends: 'browserstack',
      desiredCapabilities: {
        browserName: 'safari'
      }
    },

    'browserstack.local_chrome': {
      extends: 'browserstack.local',
      desiredCapabilities: {
        browserName: 'chrome'
      }
    },

    'browserstack.local_firefox': {
      extends: 'browserstack.local',
      desiredCapabilities: {
        browserName: 'firefox'
      }
    },
    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using the SauceLabs cloud service                      |
    //                                                                               |
    // Please set the username and access key by setting the environment variables:  |
    // - SAUCE_USERNAME                                                              |
    // - SAUCE_ACCESS_KEY                                                            |
    //////////////////////////////////////////////////////////////////////////////////
    saucelabs: {
      selenium: {
        host: 'ondemand.saucelabs.com',
        port: 443
      },
      // More info on configuring capabilities can be found on:
      // https://docs.saucelabs.com/dev/test-configuration-options/
      desiredCapabilities: {
        'sauce:options' : {
          username: '${SAUCE_USERNAME}',
          accessKey: '${SAUCE_ACCESS_KEY}',
          screenResolution: '1280x1024'
          // https://docs.saucelabs.com/dev/cli/sauce-connect-proxy/#--region
          // region: 'us-west-1'
          // https://docs.saucelabs.com/dev/test-configuration-options/#tunnelidentifier
          // parentTunnel: '',
          // tunnelIdentifier: '',
        }
      },
      disable_error_log: false,
      webdriver: {
        start_process: false
      }
    },
    'saucelabs.chrome': {
      extends: 'saucelabs',
      desiredCapabilities: {
        browserName: 'chrome',
        browserVersion: 'latest',
        javascriptEnabled: true,
        acceptSslCerts: true,
        timeZone: 'London',
        chromeOptions : {
          w3c: true
        }
      }
    },
    'saucelabs.firefox': {
      extends: 'saucelabs',
      desiredCapabilities: {
        browserName: 'firefox',
        browserVersion: 'latest',
        javascriptEnabled: true,
        acceptSslCerts: true,
        timeZone: 'London'
      }
    },
    //////////////////////////////////////////////////////////////////////////////////
    // Configuration for when using the Selenium service, either locally or remote,  |
    //  like Selenium Grid                                                           |
    //////////////////////////////////////////////////////////////////////////////////
    selenium_server: {
      // Selenium Server is running locally and is managed by Nightwatch
      // Install the NPM package @nightwatch/selenium-server or download the selenium server jar file from https://github.com/SeleniumHQ/selenium/releases/, e.g.: selenium-server-4.1.1.jar
      selenium: {
        start_process: true,
        port: 4444,
        server_path: '', // Leave empty if @nightwatch/selenium-server is installed
        command: 'standalone', // Selenium 4 only
        cli_args: {
          //'webdriver.gecko.driver': '',
          //'webdriver.chrome.driver': ''
        }
      },
      webdriver: {
        start_process: false,
        default_path_prefix: '/wd/hub'
      }
    },

    'selenium.chrome': {
      extends: 'selenium_server',
      desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions : {
          w3c: true
        }
      }
    },

    'selenium.firefox': {
      extends: 'selenium_server',
      desiredCapabilities: {
        browserName: 'firefox',
        'moz:firefoxOptions': {
          args: [
            // '-headless',
            // '-verbose'
          ]
        }
      }
    }
  }
};

Nightwatch.js Version

3.8.0

Node Version

18.20.4

Browser

Chrome 101.0.4951.41

Operating System

Ubuntu Linux, the test run in the docker container

Additional Information

No response

MurzNN commented 1 month ago

The interesting thing is that if I do this inside the Nightwath test directly - it works well! The issue appears only if I put it into the Nightwatch command and call it from the sync function.

Example of the working tests without the custom function:

module.exports = {
  'Test thFindNested in async mode': async (browser) => {
    await browser.url('http://google.com');
    const el1 = await browser.findElement('body');
    const el1Id = el1.getId();
    console.log('el1Id', el1Id);

    const els2 = await browser.elementIdElements(el1Id, 'css selector', 'div')
    const el2 = els2[0];
    console.log('el2', el2);
    const el2Id = Object.values(el2)[0];
    console.log('el2Id', el2Id);

    const els3 = await browser.elementIdElements(el2Id, 'css selector', 'div')
    console.log('els3', els3);
    const el3 = els3[0];
    console.log('el3', el3);
    const el3Id = Object.values(el3)[0];
    console.log('el3Id', el3Id);
  },

  'Test thFindNested in sync mode': (browser) => {
    browser
      .url('http://google.com')
      .findElement('body', (result) => {
        const el1 = result.value;
        const el1Id = el1.getId();
        console.log('el1Id', el1Id);

        browser.elementIdElements(el1Id, 'css selector', 'div', function (result) {
          const els2 = result.value;
          const el2 = els2[0];
          console.log('el2', el2);
          const el2Id = Object.values(el2)[0];
          console.log('el2Id', el2Id);

          browser.elementIdElements(el2Id, 'css selector', 'div', function (result) {
            const els3 = result.value;
            console.log('els3', els3);
            const el3 = els3[0];
            console.log('el3', el3);
            const el3Id = Object.values(el3)[0];
            console.log('el3Id', el3Id);
          });
        });
      })
      .end();
  },
};