nightwatch-custom-commands-assertions icon indicating copy to clipboard operation
nightwatch-custom-commands-assertions copied to clipboard

elementHasChildrenCount ignores child_selector

Open curiosity26 opened this issue 4 years ago • 1 comments

When attempting to use elementHasChildrenCount with a child selector, the value of child_selector is undefined inside the execute() function.

Steps to recreate:

The issues comes when trying to select a deep child from a parent:

<div id="parent">
    <div>
        <div class="target_child">Child 1</div>
        <div class="target_child">Child 2</div>
    </div>
</div>

Then in your test, try the following:

{
 // ...

    "Child count should be valid": browser => {
          browser.assert.elementHasChildrenCount("#parent", 2, ".target_child");
         // FAILS with count of 1
    },

// ...
}

curiosity26 avatar Apr 10 '20 15:04 curiosity26

I've written a workaround assertion for 0.9:

module.exports.assertion = function (selector, count, childSelector = null) {
  this.message = `The number of children is not ${count}`;

  this.expected = count;

  this.pass = (value) => value;

  this.value = (result) => result;

  this.command = (cb) => this.api.execute(`var element = document.querySelector("${selector}");
    var childSelector = "${childSelector}";
    
    if (childSelector) {
      return selector.querySelectorAll(childSelector).length;
    }
    
    return selector.children.length;
    `, [], ({ value }) => cb(value));
};

curiosity26 avatar Apr 10 '20 15:04 curiosity26