responsive_image_grid/tests/e2e/custom-assertions/elementCount.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-06-25 23:29:05 +02:00
/**
* A custom Nightwatch assertion. The assertion name is the filename.
*
* Example usage:
* browser.assert.elementCount(selector, count)
*
* For more information on custom assertions see:
* https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-assertions
*
*
* @param {string|object} selectorOrObject
* @param {number} count
*/
exports.assertion = function elementCount(selectorOrObject, count) {
2020-06-25 23:31:51 +02:00
let selector
2020-06-25 23:29:05 +02:00
// when called from a page object element or section
2020-06-25 23:31:51 +02:00
if (typeof selectorOrObject === 'object' && selectorOrObject.selector) {
2020-06-25 23:29:05 +02:00
// eslint-disable-next-line prefer-destructuring
2020-06-25 23:31:51 +02:00
selector = selectorOrObject.selector
2020-06-25 23:29:05 +02:00
} else {
2020-06-25 23:31:51 +02:00
selector = selectorOrObject
2020-06-25 23:29:05 +02:00
}
2020-06-25 23:31:51 +02:00
this.message = `Testing if element <${selector}> has count: ${count}`
this.expected = count
this.pass = (val) => val === count
this.value = (res) => res.value
2020-06-25 23:29:05 +02:00
function evaluator(_selector) {
2020-06-25 23:31:51 +02:00
return document.querySelectorAll(_selector).length
2020-06-25 23:29:05 +02:00
}
2020-06-25 23:31:51 +02:00
this.command = (cb) => this.api.execute(evaluator, [selector], cb)
}