diff --git a/lib/api/_loaders/static.js b/lib/api/_loaders/static.js index a8ae11be58..c22bac8286 100644 --- a/lib/api/_loaders/static.js +++ b/lib/api/_loaders/static.js @@ -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)); diff --git a/lib/api/client-commands/within.js b/lib/api/client-commands/within.js index 705fcca51d..872ecc5fda 100644 --- a/lib/api/client-commands/within.js +++ b/lib/api/client-commands/within.js @@ -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 */ diff --git a/test/sampletests/withchaiexpect/sampleWithChai.js b/test/sampletests/withchaiexpect/sampleWithChai.js index fa0ed0d385..03569cf1f7 100644 --- a/test/sampletests/withchaiexpect/sampleWithChai.js +++ b/test/sampletests/withchaiexpect/sampleWithChai.js @@ -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) { diff --git a/test/sampletests/withchaiexpect/sampleWithGlobalExpect.js b/test/sampletests/withchaiexpect/sampleWithGlobalExpect.js new file mode 100644 index 0000000000..20d53a0279 --- /dev/null +++ b/test/sampletests/withchaiexpect/sampleWithGlobalExpect.js @@ -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; + } + +}; diff --git a/test/src/runner/testRunnerChaiExpect.js b/test/src/runner/testRunnerChaiExpect.js index c3ef51cd5a..25b0c6336b 100644 --- a/test/src/runner/testRunnerChaiExpect.js +++ b/test/src/runner/testRunnerChaiExpect.js @@ -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) { @@ -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: { @@ -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); + }); + }); });