Skip to content

Commit

Permalink
fix: css and xPath selector start node is document (#2543)
Browse files Browse the repository at this point in the history
Addressing #2539

According to the spec, the default `startNodes` of the
`browsingContext.locateNodes` is `document`, not `document.body`.
  • Loading branch information
sadym-chromium authored Aug 29, 2024
1 parent 0986f86 commit b6637c1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ export class BrowsingContextImpl {
return [...element.querySelectorAll(cssSelector)];
};

startNodes = startNodes.length > 0 ? startNodes : [document.body];
startNodes = startNodes.length > 0 ? startNodes : [document];
const returnedNodes = startNodes
.map((startNode) =>
// TODO: stop search early if `maxNodeCount` is reached.
Expand Down Expand Up @@ -1298,7 +1298,7 @@ export class BrowsingContextImpl {
}
return returnedNodes;
};
startNodes = startNodes.length > 0 ? startNodes : [document.body];
startNodes = startNodes.length > 0 ? startNodes : [document];
const returnedNodes = startNodes
.map((startNode) =>
// TODO: stop search early if `maxNodeCount` is reached.
Expand Down Expand Up @@ -1530,7 +1530,12 @@ export class BrowsingContextImpl {
}
}

startNodes = startNodes.length > 0 ? startNodes : [document.body];
startNodes =
startNodes.length > 0
? startNodes
: Array.from(document.documentElement.children).filter(
(c) => c instanceof HTMLElement
);
collect(startNodes, {
role,
name,
Expand Down
66 changes: 65 additions & 1 deletion tests/browsing_context/test_locate_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import re

import pytest
from test_helpers import ANY_SHARED_ID, execute_command, goto_url
from test_helpers import ANY_SHARED_ID, AnyExtending, execute_command, goto_url


@pytest.mark.parametrize('locator', [
Expand Down Expand Up @@ -143,3 +143,67 @@ async def test_locate_nodes_locator_invalid(websocket, context_id, html,
'locator': locator
}
})


@pytest.mark.parametrize('locator', [
{
'type': 'css',
'value': 'html'
},
{
'type': 'xpath',
'value': 'html'
},
])
@pytest.mark.parametrize('start_node_expression,should_find',
[(None, True), ('document', True),
('document.body', False)])
@pytest.mark.asyncio
async def test_locate_nodes_select_html(websocket, context_id, html, locator,
start_node_expression, should_find):
await goto_url(
websocket, context_id,
html(
'<div data-class="one" aria-label="test" role="button">foobarBARbaz</div><div data-class="two" aria-label="test" role="button">foobarBAR<span>baz</span></div>'
))

start_nodes = None
if start_node_expression:
resp = await execute_command(
websocket, {
'method': 'script.evaluate',
'params': {
'expression': start_node_expression,
'target': {
'context': context_id
},
'awaitPromise': False
}
})
start_nodes = [{'sharedId': resp['result']['sharedId']}]

resp = await execute_command(
websocket, {
'method': 'browsingContext.locateNodes',
'params': {
'context': context_id,
'locator': locator,
**({
'startNodes': start_nodes
} if start_nodes else {})
}
})

if should_find:
assert resp == {
"nodes": [
AnyExtending({
'type': 'node',
'value': {
'localName': 'html',
}
})
]
}
else:
assert resp == {"nodes": []}

0 comments on commit b6637c1

Please sign in to comment.