Skip to content

Commit

Permalink
(webdriver): fix custom request error message override (#12393)
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinheitzman authored and christian-bromann committed Mar 6, 2024
1 parent 57327cd commit ad93e91
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions packages/webdriver/src/utils.ts
Expand Up @@ -248,12 +248,21 @@ export class CustomRequestError extends Error {
let errorMessage = errorObj.message || errorObj.class || 'unknown error'

/**
* improve error message for Chrome and Safari on invalid selectors
* Improve Chromedriver's error message for an invalid selector
*
* Chrome:
* error: 'invalid argument'
* message: 'invalid argument: invalid locator\n (Session info: chrome=122.0.6261.94)'
* Firefox:
* error: 'invalid selector'
* message: 'Given xpath expression "//button" is invalid: NotSupportedError: Operation is not supported'
* Safari:
* error: 'timeout'
* message: ''
*/
if (typeof errorObj.error === 'string' && errorObj.error.includes('invalid selector')) {
if (typeof errorObj.message === 'string' && errorObj.message.includes('invalid locator')) {
errorMessage = (
`The selector "${requestOptions.value}" used with strategy "${requestOptions.using}" is invalid! ` +
'For more information on selectors visit the WebdriverIO docs at: https://webdriver.io/docs/selectors'
`The selector "${requestOptions.value}" used with strategy "${requestOptions.using}" is invalid!`
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/webdriver/tests/__snapshots__/utils.test.ts.snap
@@ -1,3 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`utils > CustomRequestError 1`] = `"The selector "!!" used with strategy "css selector" is invalid! For more information on selectors visit the WebdriverIO docs at: https://webdriver.io/docs/selectors"`;
exports[`utils > CustomRequestError 1`] = `"The selector "!!" used with strategy "css selector" is invalid!"`;
2 changes: 1 addition & 1 deletion packages/webdriver/tests/utils.test.ts
Expand Up @@ -176,7 +176,7 @@ describe('utils', () => {
expect(error.stack).toMatch('unknown error')

error = new CustomRequestError(
{ value: { error: 'invalid selector' } },
{ value: { message: 'invalid locator' } },
{ using: 'css selector', value: '!!' }
)
expect(error.message).toMatchSnapshot()
Expand Down

0 comments on commit ad93e91

Please sign in to comment.