diff --git a/lib/api/web-element/scoped-element.js b/lib/api/web-element/scoped-element.js index 8dffc0b8f..234aa55a7 100644 --- a/lib/api/web-element/scoped-element.js +++ b/lib/api/web-element/scoped-element.js @@ -180,11 +180,14 @@ class ScopedWebElement { return await this.findElement({parentElement, selector: condition, index, timeout, retryInterval}); } catch (error) { - if (this.suppressNotFoundErrors) { + const narrowedError = createNarrowedError({error, condition, timeout}); + if ( + this.suppressNotFoundErrors && + narrowedError.name === 'NoSuchElementError' + ) { return null; } - const narrowedError = createNarrowedError({error, condition, timeout}); Logger.error(narrowedError); if (abortOnFailure) { @@ -356,6 +359,8 @@ function createNarrowedError({error, condition, timeout}) { return err; } + error.message = `Error occurred while trying to locate element "${condition}": ${error.message || 'unknown error'}`; + return error; } diff --git a/test/sampletests/webelement/findWithSuppressNotFoundErrors.js b/test/sampletests/webelement/findWithSuppressNotFoundErrors.js new file mode 100644 index 000000000..3f71876f7 --- /dev/null +++ b/test/sampletests/webelement/findWithSuppressNotFoundErrors.js @@ -0,0 +1,8 @@ +describe('test', function () { + test('test find with suppressNotFoundErrors', async (browser) => { + browser.element.find({ + selector: browser.globals.selector, + suppressNotFoundErrors: browser.globals.suppressNotFoundErrors + }); + }); +}); diff --git a/test/src/api/commands/web-element/testFindOnErrors.js b/test/src/api/commands/web-element/testFindOnErrors.js new file mode 100644 index 000000000..09810ed48 --- /dev/null +++ b/test/src/api/commands/web-element/testFindOnErrors.js @@ -0,0 +1,171 @@ +const assert = require('assert'); +const path = require('path'); +const MockServer = require('../../../../lib/mockserver.js'); +const Mocks = require('../../../../lib/command-mocks.js'); +const CommandGlobals = require('../../../../lib/globals/commands-w3c.js'); +const common = require('../../../../common.js'); +const NightwatchClient = common.require('index.js'); +const {settings} = common; + +describe('element().isPresent() command', function() { + before(function (done) { + CommandGlobals.beforeEach.call(this, done); + + }); + + after(function (done) { + CommandGlobals.afterEach.call(this, done); + }); + + it('test .element.find(suppressNotFoundErrors: true) suppresses NoSuchElementError', async function() { + Mocks.createNewW3CSession(); + MockServer.addMock({ + url: '/session/13521-10219-202/elements', + method: 'POST', + postdata: JSON.stringify({using: 'css selector', value: '#wrong'}), + response: JSON.stringify({ + value: [] + }) + }); + + let globalReporterCalled = false; + + const globals = { + reporter(results) { + globalReporterCalled = true; + if (Object.prototype.hasOwnProperty.call(results, 'lastError')) { + assert.notStrictEqual(results.lastError.name, 'NoSuchElementError'); + } + }, + waitForConditionTimeout: 100, + selector: '#wrong', + suppressNotFoundErrors: true + }; + const testsPath = [ + path.join(__dirname, '../../../../sampletests/webelement/findWithSuppressNotFoundErrors.js') + ]; + + await NightwatchClient.runTests(testsPath, settings({ + globals, + output_folder: 'output', + selenium_host: null + })); + + assert.strictEqual(globalReporterCalled, true); + }); + + it('test .element.find(suppressNotFoundErrors: false) does not suppress NoSuchElementError', async function() { + Mocks.createNewW3CSession(); + MockServer.addMock({ + url: '/session/13521-10219-202/elements', + method: 'POST', + postdata: JSON.stringify({using: 'css selector', value: '#wrong'}), + response: JSON.stringify({ + value: [] + }) + }); + + let globalReporterCalled = false; + + const globals = { + reporter(results) { + globalReporterCalled = true; + assert.strictEqual(results.lastError.name, 'NoSuchElementError'); + }, + waitForConditionTimeout: 100, + selector: '#wrong', + suppressNotFoundErrors: false + }; + const testsPath = [ + path.join(__dirname, '../../../../sampletests/webelement/findWithSuppressNotFoundErrors.js') + ]; + + await NightwatchClient.runTests(testsPath, settings({ + globals, + output_folder: 'output', + selenium_host: null + })); + + assert.strictEqual(globalReporterCalled, true); + }); + + it('test .element.find(suppressNotFoundErrors: true) does not suppress other errors', async function() { + Mocks.createNewW3CSession(); + MockServer.addMock({ + url: '/session/13521-10219-202/elements', + method: 'POST', + postdata: JSON.stringify({using: 'css selector', value: '@wrong'}), + response: { + value: { + error: 'invalid selector', + message: 'invalid selector', + stacktrace: '' + } + }, + statusCode: 400 + }); + + let globalReporterCalled = false; + + const globals = { + reporter(results) { + globalReporterCalled = true; + assert.strictEqual(results.lastError.name, 'InvalidSelectorError'); + }, + waitForConditionTimeout: 100, + selector: '@wrong', + suppressNotFoundErrors: true + }; + const testsPath = [ + path.join(__dirname, '../../../../sampletests/webelement/findWithSuppressNotFoundErrors.js') + ]; + + await NightwatchClient.runTests(testsPath, settings({ + globals, + output_folder: 'output', + selenium_host: null + })); + + assert.strictEqual(globalReporterCalled, true); + }); + + it('test .element.find(suppressNotFoundErrors: false) does not suppress other errors', async function() { + Mocks.createNewW3CSession(); + MockServer.addMock({ + url: '/session/13521-10219-202/elements', + method: 'POST', + postdata: JSON.stringify({using: 'css selector', value: '@wrong'}), + response: { + value: { + error: 'invalid selector', + message: 'invalid selector', + stacktrace: '' + } + }, + statusCode: 400 + }); + + let globalReporterCalled = false; + + const globals = { + reporter(results) { + globalReporterCalled = true; + assert.strictEqual(results.lastError.name, 'InvalidSelectorError'); + }, + waitForConditionTimeout: 100, + selector: '@wrong', + suppressNotFoundErrors: false + }; + const testsPath = [ + path.join(__dirname, '../../../../sampletests/webelement/findWithSuppressNotFoundErrors.js') + ]; + + await NightwatchClient.runTests(testsPath, settings({ + globals, + output_folder: 'output', + selenium_host: null + })); + + assert.strictEqual(globalReporterCalled, true); + }); +});