Skip to content

Commit

Permalink
fixed an issue with global expect() assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Jan 4, 2023
1 parent fdadd08 commit bfc2beb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/api/_loaders/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports = class StaticAssert {
try {
this.nightwatchInstance.setApiMethod('expect', parent, (...args) => {
let obj = args[0];
const isElement = Element.isElementObject(obj) || obj['@nightwatch_element'];
const isElement = Element.isElementObject(obj) || Utils.isObject(obj) && obj['@nightwatch_element'];

if (!(obj instanceof Promise) && !isElement) {
args[0] = obj = new Promise(resolve => resolve(obj));
Expand Down
3 changes: 2 additions & 1 deletion lib/api/client-commands/within.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* })
*
* @method within
* @syntax browser.within('#element')
* @syntax browser.within('#element').click('button');
* @param {string|object} selector 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).
* @api protocol.elements
* @since 2.5.5
*/
Expand Down
8 changes: 8 additions & 0 deletions test/sampletests/withchaiexpect/sampleWithChai.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ module.exports = {
client.end();
},

demoTest1: function (client) {
client.url('http://localhost');

expect(element('#weblogin')).to.be.present;

client.end();
},

demoTest2: function (client) {
client.url('http://localhost')
.elements('css selector', '#weblogin', async function (result) {
Expand Down
14 changes: 14 additions & 0 deletions test/sampletests/withchaiexpect/sampleWithGlobalExpect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
demoTest: async function (client) {
const result = await client.url('http://localhost')
.elements('css selector', '#weblogin');

expect(result).to.have.length(1);
expect(null).to.be.null;
},

demoTestWithError: function () {
expect('#weblogin').to.be.present;
}

};
40 changes: 37 additions & 3 deletions test/src/runner/testRunnerChaiExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert');
const common = require('../../common.js');
const MockServer = require('../../lib/mockserver.js');
const CommandGlobals = require('../../lib/globals/commands.js');
const Globals = require("../../lib/globals");

describe('testRunnerChaiExpect', function() {
before(function(done) {
Expand All @@ -18,7 +19,7 @@ describe('testRunnerChaiExpect', function() {
});

it('testRunWithChaiExpect', function() {
const testsPath = path.join(__dirname, '../../sampletests/withchaiexpect');
const testsPath = path.join(__dirname, '../../sampletests/withchaiexpect/sampleWithChai.js');
const Settings = common.require('settings/settings.js');
let settings = Settings.parse({
selenium: {
Expand All @@ -45,11 +46,44 @@ describe('testRunnerChaiExpect', function() {
assert.ok(runner.results.lastError instanceof Error);

const ex = runner.results.lastError;

assert.ok(ex.message.startsWith('expected [ { ELEMENT: \'0\' } ] to have a length of 2 but got 1'));

assert.strictEqual(runner.results.modules.sampleWithChai.tests, 2);
assert.strictEqual(runner.results.modules.sampleWithChai.tests, 3);
assert.strictEqual(runner.results.modules.sampleWithChai.failures, 1);
});
});

it('test run with global expect()', function() {
const testsPath = path.join(__dirname, '../../sampletests/withchaiexpect/sampleWithGlobalExpect.js');

const Settings = common.require('settings/settings.js');
let settings = Settings.parse({
selenium: {
port: 10195,
host: 'localhost',
start_process: false
},
globals: {
test: assert,
reporter() {
}
},
output_folder: false,
silent: false,
output: false
});

const Globals = require('../../lib/globals.js');

return Globals.startTestRunner(testsPath, settings)
.then(runner => {
assert.ok(runner.results.lastError instanceof Error);

const ex = runner.results.lastError;
assert.strictEqual(ex.message, 'Property ".present" is not available when asserting on non-element values.');

assert.strictEqual(runner.results.modules.sampleWithGlobalExpect.tests, 2);
assert.strictEqual(runner.results.modules.sampleWithGlobalExpect.errors, 1);
});
});
});

0 comments on commit bfc2beb

Please sign in to comment.