From 54bf7edb559814951ad7a3cbdaf7e1255e11d50e Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Mon, 11 Mar 2024 12:07:31 +0100 Subject: [PATCH 1/4] feat: `invalid selector` error code --- src/protocol-parser/generated/webdriver-bidi.ts | 1 + src/protocol/ErrorResponse.spec.ts | 1 + src/protocol/ErrorResponse.ts | 6 ++++++ src/protocol/generated/webdriver-bidi.ts | 1 + 4 files changed, 9 insertions(+) diff --git a/src/protocol-parser/generated/webdriver-bidi.ts b/src/protocol-parser/generated/webdriver-bidi.ts index dfc6219073..19af7b2038 100644 --- a/src/protocol-parser/generated/webdriver-bidi.ts +++ b/src/protocol-parser/generated/webdriver-bidi.ts @@ -111,6 +111,7 @@ export const JsUintSchema = z export const ErrorCodeSchema = z.lazy(() => z.enum([ 'invalid argument', + 'invalid selector', 'invalid session id', 'move target out of bounds', 'no such alert', diff --git a/src/protocol/ErrorResponse.spec.ts b/src/protocol/ErrorResponse.spec.ts index 6e71d691a8..32edb1785b 100644 --- a/src/protocol/ErrorResponse.spec.ts +++ b/src/protocol/ErrorResponse.spec.ts @@ -25,6 +25,7 @@ describe('Exception', () => { } = { // keep-sorted start [ErrorCode.InvalidArgument]: undefined, + [ErrorCode.InvalidSelector]: undefined, [ErrorCode.InvalidSessionId]: undefined, [ErrorCode.MoveTargetOutOfBounds]: undefined, [ErrorCode.NoSuchAlert]: undefined, diff --git a/src/protocol/ErrorResponse.ts b/src/protocol/ErrorResponse.ts index 762dbcbfac..066e635cb3 100644 --- a/src/protocol/ErrorResponse.ts +++ b/src/protocol/ErrorResponse.ts @@ -41,6 +41,12 @@ export class InvalidArgumentException extends Exception { } } +export class InvalidSelectorException extends Exception { + constructor(message: string, stacktrace?: string) { + super(ErrorCode.InvalidSelector, message, stacktrace); + } +} + export class InvalidSessionIdException extends Exception { constructor(message: string, stacktrace?: string) { super(ErrorCode.InvalidSessionId, message, stacktrace); diff --git a/src/protocol/generated/webdriver-bidi.ts b/src/protocol/generated/webdriver-bidi.ts index 9678269965..a381ba00f4 100644 --- a/src/protocol/generated/webdriver-bidi.ts +++ b/src/protocol/generated/webdriver-bidi.ts @@ -79,6 +79,7 @@ export type JsInt = number; export type JsUint = number; export const enum ErrorCode { InvalidArgument = 'invalid argument', + InvalidSelector = 'invalid selector', InvalidSessionId = 'invalid session id', MoveTargetOutOfBounds = 'move target out of bounds', NoSuchAlert = 'no such alert', From ca21e34f63c0bb1f96dc2c53040614e3fb0fbe31 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Mon, 11 Mar 2024 13:00:04 +0100 Subject: [PATCH 2/4] feat: `invalid selector` error --- .../domains/context/BrowsingContextImpl.ts | 11 +++++++++ tests/browsing_context/test_locate_nodes.py | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/bidiMapper/domains/context/BrowsingContextImpl.ts b/src/bidiMapper/domains/context/BrowsingContextImpl.ts index 10d9c2d0de..c96e563f9c 100644 --- a/src/bidiMapper/domains/context/BrowsingContextImpl.ts +++ b/src/bidiMapper/domains/context/BrowsingContextImpl.ts @@ -22,6 +22,7 @@ import { BrowsingContext, ChromiumBidi, InvalidArgumentException, + InvalidSelectorException, NoSuchElementException, NoSuchHistoryEntryException, Script, @@ -1083,6 +1084,16 @@ export class BrowsingContextImpl { ); if (selectorScriptResult.type !== 'success') { + // Heuristic to detect invalid selector. + if ( + selectorScriptResult.exceptionDetails.text?.endsWith( + 'is not a valid selector.' + ) + ) { + throw new InvalidSelectorException( + `Not valid css selector ${selector}` + ); + } throw new UnknownErrorException( `Unexpected error in selector script: ${selectorScriptResult.exceptionDetails.text}` ); diff --git a/tests/browsing_context/test_locate_nodes.py b/tests/browsing_context/test_locate_nodes.py index a5c210f7c5..7831fb57d1 100644 --- a/tests/browsing_context/test_locate_nodes.py +++ b/tests/browsing_context/test_locate_nodes.py @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import re import pytest from test_helpers import ANY_SHARED_ID, execute_command, goto_url @@ -68,3 +69,26 @@ async def test_locate_nodes_css_locator(websocket, context_id, html): }, ] } + + +@pytest.mark.asyncio +async def test_locate_nodes_css_locator_invalid(websocket, context_id, html): + invalid_css_selector = 'a*b' + with pytest.raises(Exception, + match=re.escape( + str({ + 'error': 'invalid selector', + 'message': 'Not valid css selector ' + + invalid_css_selector + }))): + await execute_command( + websocket, { + 'method': 'browsingContext.locateNodes', + 'params': { + 'context': context_id, + 'locator': { + 'type': 'css', + 'value': invalid_css_selector + } + } + }) From 7aea11a8dea5ecdb56a0290708671b77be66bf4e Mon Sep 17 00:00:00 2001 From: sadym-chromium Date: Mon, 11 Mar 2024 12:09:05 +0000 Subject: [PATCH 3/4] Update WPT expectations Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../tests/bidi/browsing_context/locate_nodes/invalid.py.ini | 3 --- .../tests/bidi/browsing_context/locate_nodes/invalid.py.ini | 3 --- .../tests/bidi/browsing_context/locate_nodes/invalid.py.ini | 3 --- 3 files changed, 9 deletions(-) diff --git a/wpt-metadata/chromedriver/headful/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini b/wpt-metadata/chromedriver/headful/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini index 41ae67e656..55fa5aa47a 100644 --- a/wpt-metadata/chromedriver/headful/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini +++ b/wpt-metadata/chromedriver/headful/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini @@ -1,7 +1,4 @@ [invalid.py] - [test_params_locator_value_invalid_value[css-a*b\]] - expected: FAIL - [test_params_locator_value_invalid_value[xpath-\]] expected: FAIL diff --git a/wpt-metadata/chromedriver/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini b/wpt-metadata/chromedriver/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini index 41ae67e656..55fa5aa47a 100644 --- a/wpt-metadata/chromedriver/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini +++ b/wpt-metadata/chromedriver/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini @@ -1,7 +1,4 @@ [invalid.py] - [test_params_locator_value_invalid_value[css-a*b\]] - expected: FAIL - [test_params_locator_value_invalid_value[xpath-\]] expected: FAIL diff --git a/wpt-metadata/mapper/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini b/wpt-metadata/mapper/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini index 41ae67e656..55fa5aa47a 100644 --- a/wpt-metadata/mapper/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini +++ b/wpt-metadata/mapper/headless/webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py.ini @@ -1,7 +1,4 @@ [invalid.py] - [test_params_locator_value_invalid_value[css-a*b\]] - expected: FAIL - [test_params_locator_value_invalid_value[xpath-\]] expected: FAIL From 4e37d1870acb4211af45c2833fec3678a0db2dc9 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Mon, 11 Mar 2024 13:36:07 +0100 Subject: [PATCH 4/4] update error message --- src/bidiMapper/domains/context/BrowsingContextImpl.ts | 7 ++++--- tests/browsing_context/test_locate_nodes.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bidiMapper/domains/context/BrowsingContextImpl.ts b/src/bidiMapper/domains/context/BrowsingContextImpl.ts index c96e563f9c..fc11c8384b 100644 --- a/src/bidiMapper/domains/context/BrowsingContextImpl.ts +++ b/src/bidiMapper/domains/context/BrowsingContextImpl.ts @@ -1086,13 +1086,14 @@ export class BrowsingContextImpl { if (selectorScriptResult.type !== 'success') { // Heuristic to detect invalid selector. if ( + selectorScriptResult.exceptionDetails.text?.startsWith( + 'SyntaxError:' + ) && selectorScriptResult.exceptionDetails.text?.endsWith( 'is not a valid selector.' ) ) { - throw new InvalidSelectorException( - `Not valid css selector ${selector}` - ); + throw new InvalidSelectorException(`Not valid selector ${selector}`); } throw new UnknownErrorException( `Unexpected error in selector script: ${selectorScriptResult.exceptionDetails.text}` diff --git a/tests/browsing_context/test_locate_nodes.py b/tests/browsing_context/test_locate_nodes.py index 7831fb57d1..d1f5917d0d 100644 --- a/tests/browsing_context/test_locate_nodes.py +++ b/tests/browsing_context/test_locate_nodes.py @@ -78,7 +78,7 @@ async def test_locate_nodes_css_locator_invalid(websocket, context_id, html): match=re.escape( str({ 'error': 'invalid selector', - 'message': 'Not valid css selector ' + + 'message': 'Not valid selector ' + invalid_css_selector }))): await execute_command(