Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4223: Restrict suppressNotFoundErrors to NoSuchElementError #4294

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions test/sampletests/webelement/findWithSuppressNotFoundErrors.js
Original file line number Diff line number Diff line change
@@ -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
});
});
});
171 changes: 171 additions & 0 deletions test/src/api/commands/web-element/testFindOnErrors.js
Original file line number Diff line number Diff line change
@@ -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);
});
});