github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/tests/e2e/custom-assertions/elementCount.js (about) 1 /** 2 * A custom Nightwatch assertion. The assertion name is the filename. 3 * 4 * Example usage: 5 * browser.assert.elementCount(selector, count) 6 * 7 * For more information on custom assertions see: 8 * https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-assertions 9 * 10 * 11 * @param {string|object} selectorOrObject 12 * @param {number} count 13 */ 14 15 exports.assertion = function elementCount(selectorOrObject, count) { 16 let selector; 17 18 // when called from a page object element or section 19 if (typeof selectorOrObject === 'object' && selectorOrObject.selector) { 20 // eslint-disable-next-line prefer-destructuring 21 selector = selectorOrObject.selector; 22 } else { 23 selector = selectorOrObject; 24 } 25 26 this.message = `Testing if element <${selector}> has count: ${count}`; 27 this.expected = count; 28 this.pass = val => val === count; 29 this.value = res => res.value; 30 function evaluator(_selector) { 31 return document.querySelectorAll(_selector).length; 32 } 33 this.command = cb => this.api.execute(evaluator, [selector], cb); 34 };