nightwatch
Version:
Easy to use Node.js based end-to-end testing solution for web applications using the W3C WebDriver API.
57 lines (47 loc) • 1.86 kB
JavaScript
/**
* Checks if the given attribute of an element contains the expected value.
*
* @example
* this.demoTest = function (browser) {
* browser.assert.attributeContains('#someElement', 'href', 'google.com');
* };
*
* @method assert.attributeContains
* @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/writing-tests/finding-interacting-with-dom-elements.html#postdoc-element-properties).
* @param {string} attribute The attribute name
* @param {string} expected The expected contained value of the attribute to check.
* @param {string} [msg] Optional log message to display in the output. If missing, one is displayed by default.
* @api assertions
*/
const {setElementSelectorProps} = require('../../utils');
exports.assertion = function(definition, attribute, expected, msg) {
this.options = {
elementSelector: true
};
this.formatMessage = function() {
const message = msg || `Testing if attribute %s of element %s ${this.negate ? 'doesn\'t contain %s' : 'contains %s'}`;
return {
message,
args: [`'${attribute}'`, this.elementSelector, `'${expected}'`]
};
};
this.evaluate = function(value) {
value = value || '';
return value.includes(expected);
};
this.actual = function() {
const value = this.getValue();
if (typeof value != 'string') {
return `Element does not have a '${attribute}' attribute`;
}
return value;
};
this.expected = function() {
return this.negate ? `not contains '${expected}'` : `contains '${expected}'`;
};
this.command = function(callback) {
return this.api.getAttribute(setElementSelectorProps(definition, {
suppressNotFoundErrors: true
}), attribute, callback);
};
};