Skip to content
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
13 changes: 12 additions & 1 deletion javascript/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,9 @@ class WebDriver {
async findElementInternal_(locatorFn, context) {
let result = await locatorFn(context)
if (Array.isArray(result)) {
if (result.length === 0) {
throw new NoSuchElementError('Cannot locate an element with provided parameters')
}
result = result[0]
}
if (!(result instanceof WebElement)) {
Expand Down Expand Up @@ -1088,7 +1091,15 @@ class WebDriver {
* @private
*/
async findElementsInternal_(locatorFn, context) {
const result = await locatorFn(context)
let result
try {
result = await locatorFn(context)
} catch (ex) {
if (ex instanceof NoSuchElementError) {
return []
}
throw ex
}
if (result instanceof WebElement) {
return [result]
}
Expand Down
23 changes: 23 additions & 0 deletions javascript/selenium-webdriver/test/lib/webdriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ describe('WebDriver', function () {
assert.strictEqual('Custom locator did not return a WebElement', e.message)
})
})

it('customLocatorThrowsNoSuchElementErrorForEmptyArray', function () {
const driver = new FakeExecutor().createDriver()
return driver
.findElement((_) => [])
.then(assert.fail, function (e) {
assertIsInstance(error.NoSuchElementError, e)
})
})
})

describe('findElements', function () {
Expand Down Expand Up @@ -854,6 +863,20 @@ describe('WebDriver', function () {
})
.then((actual) => assert.deepStrictEqual(['one', 'two'], actual))
})

it('customLocatorReturnsEmptyArrayForNoSuchElementError', function () {
const driver = new FakeExecutor().createDriver()
return driver
.findElements((_) => {
throw new error.NoSuchElementError()
})
.then((actual) => assert.deepStrictEqual([], actual))
})

it('customLocatorPropagatesUnexpectedErrors', function () {
const driver = new FakeExecutor().createDriver()
return driver.findElements(throwStubError).then(assert.fail, assertIsStubError)
})
})

describe('sendKeys', function () {
Expand Down