Skip to content

Commit

Permalink
Restrict suppressNotFoundErrors to NoSuchElementError in new Elem…
Browse files Browse the repository at this point in the history
… API. (#4294)

Co-authored-by: Priyansh Garg <[email protected]>
  • Loading branch information
Vaibhavsahu2810 and garg3133 authored Nov 27, 2024
1 parent 1d6e122 commit 2f9fcf8
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 2 deletions.
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);
});
});

0 comments on commit 2f9fcf8

Please sign in to comment.