Skip to content

Commit bfc2beb

Browse files
committed
fixed an issue with global expect() assertions
1 parent fdadd08 commit bfc2beb

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

lib/api/_loaders/static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ module.exports = class StaticAssert {
230230
try {
231231
this.nightwatchInstance.setApiMethod('expect', parent, (...args) => {
232232
let obj = args[0];
233-
const isElement = Element.isElementObject(obj) || obj['@nightwatch_element'];
233+
const isElement = Element.isElementObject(obj) || Utils.isObject(obj) && obj['@nightwatch_element'];
234234

235235
if (!(obj instanceof Promise) && !isElement) {
236236
args[0] = obj = new Promise(resolve => resolve(obj));

lib/api/client-commands/within.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* })
1212
*
1313
* @method within
14-
* @syntax browser.within('#element')
14+
* @syntax browser.within('#element').click('button');
15+
* @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).
1516
* @api protocol.elements
1617
* @since 2.5.5
1718
*/

test/sampletests/withchaiexpect/sampleWithChai.js

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ module.exports = {
55
client.end();
66
},
77

8+
demoTest1: function (client) {
9+
client.url('http://localhost');
10+
11+
expect(element('#weblogin')).to.be.present;
12+
13+
client.end();
14+
},
15+
816
demoTest2: function (client) {
917
client.url('http://localhost')
1018
.elements('css selector', '#weblogin', async function (result) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
demoTest: async function (client) {
3+
const result = await client.url('http://localhost')
4+
.elements('css selector', '#weblogin');
5+
6+
expect(result).to.have.length(1);
7+
expect(null).to.be.null;
8+
},
9+
10+
demoTestWithError: function () {
11+
expect('#weblogin').to.be.present;
12+
}
13+
14+
};

test/src/runner/testRunnerChaiExpect.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require('assert');
33
const common = require('../../common.js');
44
const MockServer = require('../../lib/mockserver.js');
55
const CommandGlobals = require('../../lib/globals/commands.js');
6+
const Globals = require("../../lib/globals");
67

78
describe('testRunnerChaiExpect', function() {
89
before(function(done) {
@@ -18,7 +19,7 @@ describe('testRunnerChaiExpect', function() {
1819
});
1920

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

4748
const ex = runner.results.lastError;
48-
4949
assert.ok(ex.message.startsWith('expected [ { ELEMENT: \'0\' } ] to have a length of 2 but got 1'));
5050

51-
assert.strictEqual(runner.results.modules.sampleWithChai.tests, 2);
51+
assert.strictEqual(runner.results.modules.sampleWithChai.tests, 3);
5252
assert.strictEqual(runner.results.modules.sampleWithChai.failures, 1);
5353
});
5454
});
55+
56+
it('test run with global expect()', function() {
57+
const testsPath = path.join(__dirname, '../../sampletests/withchaiexpect/sampleWithGlobalExpect.js');
58+
59+
const Settings = common.require('settings/settings.js');
60+
let settings = Settings.parse({
61+
selenium: {
62+
port: 10195,
63+
host: 'localhost',
64+
start_process: false
65+
},
66+
globals: {
67+
test: assert,
68+
reporter() {
69+
}
70+
},
71+
output_folder: false,
72+
silent: false,
73+
output: false
74+
});
75+
76+
const Globals = require('../../lib/globals.js');
77+
78+
return Globals.startTestRunner(testsPath, settings)
79+
.then(runner => {
80+
assert.ok(runner.results.lastError instanceof Error);
81+
82+
const ex = runner.results.lastError;
83+
assert.strictEqual(ex.message, 'Property ".present" is not available when asserting on non-element values.');
84+
85+
assert.strictEqual(runner.results.modules.sampleWithGlobalExpect.tests, 2);
86+
assert.strictEqual(runner.results.modules.sampleWithGlobalExpect.errors, 1);
87+
});
88+
});
5589
});

0 commit comments

Comments
 (0)