Skip to content

Commit

Permalink
improved messages for elementsCount and textEqual assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Feb 7, 2022
1 parent ed04684 commit 3564fb1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
16 changes: 8 additions & 8 deletions lib/api/assertions/elementsCount.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* 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.
Expand All @@ -17,21 +17,21 @@

const Element = require('../../element');

exports.assertion = function (definition, count, msg){
exports.assertion = function (definition, count, msg) {
this.options = {
elementSelector: true
};

this.expected = function() {
return this.negate ? `does not counts '${count}'` : `counts '${count}'`;
return this.negate ? `does not count ${count}` : `counts ${count}`;
};

this.formatMessage = function() {
const message = msg || `Testing if the Element ${this.negate ? 'doesn\'t counts %s' : 'counts %s'}`;
const message = msg || `Testing if the element count for %s ${this.negate ? 'is not %s' : 'is %s'}`;

return {
message,
args: [this.elementSelector, `'${count}'`]
args: [this.elementSelector, count]
};
};

Expand All @@ -45,7 +45,7 @@ exports.assertion = function (definition, count, msg){
}

return result.value.length;

};

this.createElement = function(selector, using = this.client.locateStrategy) {
Expand Down
15 changes: 10 additions & 5 deletions lib/api/assertions/textEquals.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ exports.assertion = function(definition, expected, msg) {
this.failure = function(result) {
return result === false || result && result.status === -1;
};


this.actual = function(passed) {
const value = this.getValue();
if (typeof value != 'string') {
return `Element does not have an innerText attribute`;
}

return value;
};

this.value = function(result) {
if (result.status === -1) {
return null;
Expand All @@ -56,10 +65,6 @@ exports.assertion = function(definition, expected, msg) {
return result.value;
};

this.actual = function(passed) {
return passed ? `equals '${expected}'` : `doesn't equal '${expected}'`;
};

this.command = function(callback) {
this.api.getText(setElementSelectorProps(definition, {
suppressNotFoundErrors: true
Expand Down
12 changes: 6 additions & 6 deletions test/src/api/assertions/testTextEquals.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('assert.textEquals', function () {

assert.strictEqual(instance.hasFailure(), false);
assert.strictEqual(instance.getValue(), 'other text');
assert.strictEqual(instance.getActual(), 'doesn\'t equal \'text result\'');
assert.strictEqual(instance.getActual(), 'other text');
assert.strictEqual(instance.message, 'Test message');
}
});
Expand All @@ -57,8 +57,8 @@ describe('assert.textEquals', function () {
assert.strictEqual(queueOpts.negate, true);
assert.strictEqual(instance.hasFailure(), false);
assert.strictEqual(instance.getValue(), 'text result');
assert.strictEqual(instance.getActual(), 'equals \'text result\'');
assert.strictEqual(err.message, `Test message in 5ms - expected "doesn't equal 'text result'" but got: "equals 'text result'" (${instance.elapsedTime}ms)`);
assert.strictEqual(instance.getActual(), 'text result');
assert.strictEqual(err.message, `Test message in 5ms - expected "doesn't equal 'text result'" but got: "text result" (${instance.elapsedTime}ms)`);
}
});
});
Expand All @@ -74,7 +74,7 @@ describe('assert.textEquals', function () {
assertion({instance, failure, message, err}) {
assert.ok(message.startsWith('Testing if element\'s <.test_element> inner text equals \'text result\''), message);
assert.deepStrictEqual(instance.options, {elementSelector: true});
assert.strictEqual(instance.getActual(), 'equals \'text result\'');
assert.strictEqual(instance.getActual(), 'text result');
assert.strictEqual(instance.hasFailure(), false);
assert.ok(message.startsWith('Testing if element\'s <.test_element> inner text equals \'text result\''), message);
assert.strictEqual(failure, false);
Expand All @@ -91,8 +91,8 @@ describe('assert.textEquals', function () {
assertError: true,
assertResult: true,
assertion({instance, failure}) {
assert.strictEqual(instance.getActual(), 'doesn\'t equal \'text result\'');
assert.strictEqual(failure, 'Expected "equals \'text result\'" but got: "doesn\'t equal \'text result\'"');
assert.strictEqual(instance.getActual(), 'not_expected');
assert.strictEqual(failure, 'Expected "equals \'text result\'" but got: "not_expected"');
}
});
});
Expand Down

0 comments on commit 3564fb1

Please sign in to comment.