nightwatch-custom-commands-assertions
nightwatch-custom-commands-assertions copied to clipboard
elementHasChildrenCount ignores child_selector
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
},
// ...
}
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));
};