UNPKG

nightwatch

Version:

Easy to use Node.js based end-to-end testing solution for web applications using the W3C WebDriver API.

65 lines (50 loc) 1.73 kB
/** * Checks if the number of elements specified by a selector is equal to a given value. * * @example * * this.demoTest = function (browser) { * browser.assert.elementsCount('div', 10); * browser.assert.not.elementsCount('div', 10); * } * * @method elementsCount * @param {string|object} definition The selector (CSS/Xpath) used to locate the element. Can either be a string or an object which specifies [element properties](https://nightwatchjs.org/guide#element-properties). * @param {string} count expected number of elements to be present. * @param {string} [msg] Optional log message to display in the output. If missing, one is displayed by default. * @api assertions */ const Element = require('../../element'); exports.assertion = function (definition, count, msg) { this.options = { elementSelector: true }; this.expected = function() { return this.negate ? `does not count ${count}` : `counts ${count}`; }; this.formatMessage = function() { const message = msg || `Testing if the element count for %s ${this.negate ? 'is not %s' : 'is %s'}`; return { message, args: [this.elementSelector, count] }; }; this.evaluate = function(value) { return value === count; }; this.value = function(result = {}) { if (!result || !result.value) { return ''; } return result.value.length; }; this.createElement = function(selector, using = this.client.locateStrategy) { this.element = Element.createFromSelector(selector, using); return this; }; this.command = async function(callback) { this.createElement(definition); const {element} = this; await this.api.findElements(element, callback); }; };