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(element-finder): handle element not found errors correctly #4079 #4100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/wali/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('Element Finder', function () {
before(function (browser) {
browser.navigateTo('https://github.com/login');
});

it('should handle elements not found', function(browser) {
const result = browser
.waitForElementVisible('body')
.element('.auth-form-header2') // Wrong selector name
.getProperty('innerHTML');

browser.expect.element('.auth-form-header2').to.not.be.present.before(1000);

const chained = browser
.element('.invalid-selector')
.find('.another-invalid-selector')
.getText();

browser.expect(chained).to.be.null;

browser.expect.element('.non-existent-element').to.not.be.present.before(1000);
});

it('should abort test case when abortOnFailure is set', function (browser) {
browser.options.abortOnFailure = true; // Set abortOnFailure option

const result = browser
.waitForElementVisible('body')
.element('.auth-form-header2') // Wrong selector name
.getProperty('innerHTML');

browser.expect.element('.auth-form-header2').to.not.be.present.before(1000);

// This line should not be reached if the test case is aborted
browser.assert.fail('Test case should have been aborted');
});

after(function (browser) {
browser.end();
});
});
14 changes: 7 additions & 7 deletions lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,18 @@ class ScopedWebElement {
const webElements = await this.locateElements({parentElement, selector, timeout, retryInterval});

if (webElements.length === 0) {
const err = new Error(`Cannot find element with "${selector}" selector in ${timeout} milliseconds.`);
const err = new Error(`Cannot find element with "${selector}" selector.`);
err.name = 'NoSuchElementError';
this.reporter.registerTestError(err);

return;
return null;
}

if (index > webElements.length) {
const err = new Error(`The index "${index}" is out of bounds for selector "${selector}".`);
this.reporter.registerTestError(err);

return;
return null;
}

return webElements[index];
Expand Down Expand Up @@ -175,8 +176,7 @@ class ScopedWebElement {

if (abortOnFailure) {
this.reporter.registerTestError(narrowedError);
// TODO: find a way to reject here without unhandled promise rejection
// reject(narrowedError);
throw narrowedError;
}

return null;
Expand Down Expand Up @@ -323,8 +323,8 @@ class ScopedWebElement {
}

function createNarrowedError({error, condition, timeout}) {
return error.name === 'TimeoutError'
? new Error(`Timed out while waiting for element "${condition}" to be present for ${timeout} milliseconds.`)
return error.name === 'NoSuchElementError'
? new Error(`Cannot find element with "${condition}" selector.`)
: error;
}

Expand Down