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

feat: invalid selector error #1985

Merged
merged 5 commits into from
Mar 11, 2024
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
12 changes: 12 additions & 0 deletions src/bidiMapper/domains/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BrowsingContext,
ChromiumBidi,
InvalidArgumentException,
InvalidSelectorException,
NoSuchElementException,
NoSuchHistoryEntryException,
Script,
Expand Down Expand Up @@ -1083,6 +1084,17 @@ 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 selector ${selector}`);
}
throw new UnknownErrorException(
`Unexpected error in selector script: ${selectorScriptResult.exceptionDetails.text}`
);
Expand Down
1 change: 1 addition & 0 deletions src/protocol-parser/generated/webdriver-bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/protocol/ErrorResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Exception', () => {
} = {
// keep-sorted start
[ErrorCode.InvalidArgument]: undefined,
[ErrorCode.InvalidSelector]: undefined,
[ErrorCode.InvalidSessionId]: undefined,
[ErrorCode.MoveTargetOutOfBounds]: undefined,
[ErrorCode.NoSuchAlert]: undefined,
Expand Down
6 changes: 6 additions & 0 deletions src/protocol/ErrorResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/protocol/generated/webdriver-bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions tests/browsing_context/test_locate_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 selector ' +
invalid_css_selector
}))):
await execute_command(
websocket, {
'method': 'browsingContext.locateNodes',
'params': {
'context': context_id,
'locator': {
'type': 'css',
'value': invalid_css_selector
}
}
})
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading