Bug 1855023 - [wdspec] Add more tests for "browsingContext.locateNodes" command. r=webdriver-reviewers,jdescottes,whimboo

Differential Revision: https://phabricator.services.mozilla.com/D196743
This commit is contained in:
Alexandra Borovova 2023-12-20 16:47:54 +00:00
parent f3fc5ba288
commit fe1a2465a2
5 changed files with 278 additions and 33 deletions

View file

@ -1,9 +1,9 @@
[start_nodes.py]
[test_locate_with_context_nodes[xpath-//p\]]
[test_locate_with_context_nodes[xpath-//p-expected3\]]
bug: 1869536
expected: FAIL
[test_locate_with_context_nodes[innerText-foo\]]
[test_locate_with_context_nodes[innerText-foo-expected4\]]
bug: 1869538
expected: FAIL

View file

@ -1,6 +1,8 @@
import pytest
import webdriver.bidi.error as error
from ... import any_string, recursive_compare
@pytest.mark.asyncio
async def test_params_context_invalid_value(bidi_session, inline, top_context):
@ -13,3 +15,74 @@ async def test_params_context_invalid_value(bidi_session, inline, top_context):
await bidi_session.browsing_context.locate_nodes(
context="foo", locator={ "type": "css", "value": "div" }
)
@pytest.mark.asyncio
async def test_locate_in_different_contexts(bidi_session, inline, top_context, new_tab):
url = inline("""<div class="in-top-context">foo</div>""")
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
# Try to locate nodes in the other context
result = await bidi_session.browsing_context.locate_nodes(
context=new_tab["context"], locator={"type": "css", "value": ".in-top-context"}
)
assert result["nodes"] == []
# Locate in the correct context
result = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"], locator={"type": "css", "value": ".in-top-context"}
)
expected = [
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"class": "in-top-context"},
"childNodeCount": 1,
"localName": "div",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}
]
recursive_compare(expected, result["nodes"])
@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"])
@pytest.mark.asyncio
async def test_locate_in_iframe(bidi_session, inline, top_context, domain):
iframe_url_1 = inline("<div id='in-iframe'>foo</div>", domain=domain)
page_url = inline(f"<iframe src='{iframe_url_1}'></iframe>")
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=page_url, wait="complete"
)
contexts = await bidi_session.browsing_context.get_tree(root=top_context["context"])
iframe_context = contexts[0]["children"][0]
result = await bidi_session.browsing_context.locate_nodes(
context=iframe_context["context"],
locator={"type": "css", "value": "#in-iframe"}
)
expected = [
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"id": "in-iframe"},
"childNodeCount": 1,
"localName": "div",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}
]
recursive_compare(expected, result["nodes"])

View file

@ -1,6 +1,8 @@
import pytest
import webdriver.bidi.error as error
from webdriver.bidi.modules.script import ContextTarget
pytestmark = pytest.mark.asyncio
@ -151,3 +153,68 @@ async def test_params_start_nodes_empty_list(bidi_session, inline, top_context):
locator={ "type": "css", "value": "div" },
start_nodes=[]
)
@pytest.mark.parametrize(
"value",
[
{"type": "number", "value": 3},
{"type": "window"},
{"type": "array", "value": ["test"]},
{
"type": "object",
"value": [
["1", {"type": "string", "value": "foo"}],
],
},
],
)
async def test_params_start_nodes_not_dom_node(
bidi_session, inline, top_context, value
):
await navigate_to_page(bidi_session, inline, top_context)
if value["type"] == "window":
value["value"] = top_context["context"]
with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "css", "value": "div"},
start_nodes=[value],
)
@pytest.mark.parametrize(
"expression",
[
"document.querySelector('input#button').attributes[0]",
"document.querySelector('#with-text-node').childNodes[0]",
"""document.createProcessingInstruction("xml-stylesheet", "href='foo.css'")""",
"document.querySelector('#with-comment').childNodes[0]",
"document.doctype",
"document.getElementsByTagName('div')",
"document.querySelectorAll('div')"
],
)
async def test_params_start_nodes_dom_node_not_element(
bidi_session, inline, top_context, get_test_page, expression
):
await navigate_to_page(bidi_session, inline, top_context)
await bidi_session.browsing_context.navigate(
context=top_context['context'], url=get_test_page(), wait="complete"
)
remote_reference = await bidi_session.script.evaluate(
expression=expression,
await_promise=False,
target=ContextTarget(top_context["context"]),
)
with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "css", "value": "div"},
start_nodes=[remote_reference],
)

View file

@ -137,3 +137,45 @@ async def test_find_by_locator_limit_return_count(bidi_session, inline, top_cont
)
recursive_compare(expected, result["nodes"])
@pytest.mark.asyncio
async def test_several_context_nodes(bidi_session, inline, top_context):
url = inline(
"""
<div class="context-node">
<div>should be returned</div>
</div>
<div class="context-node">
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
<div>should not be returned</div>
</div>
"""
)
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
result_context_nodes = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "css", "value": ".context-node"},
)
result = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "css", "value": "div"},
max_node_count=1,
start_nodes=[
{"sharedId": result_context_nodes["nodes"][0]["sharedId"]},
{"sharedId": result_context_nodes["nodes"][1]["sharedId"]},
],
)
assert len(result["nodes"]) == 1

View file

@ -4,35 +4,8 @@ from webdriver.bidi.modules.script import ContextTarget
from ... import any_string, recursive_compare
@pytest.mark.parametrize("type,value", [
("css", "p"),
("xpath", "//p"),
("innerText", "foo")
])
@pytest.mark.asyncio
async def test_locate_with_context_nodes(bidi_session, inline, top_context, type, value):
url = inline("""<div id="parent">
<p data-class="one">foo</p>
<p data-class="two">foo</p>
</div>""")
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
context_nodes = await bidi_session.script.evaluate(
expression="""document.querySelector("div")""",
target=ContextTarget(top_context["context"]),
await_promise=True,
)
result = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={ "type": type, "value": value },
start_nodes=[context_nodes]
)
expected = [
{
@pytest.mark.parametrize("type,value,expected", [
("css", "p", [{
"type": "node",
"sharedId": any_string,
"value": {
@ -53,8 +26,98 @@ async def test_locate_with_context_nodes(bidi_session, inline, top_context, type
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}
]
}]),
("css", "a span", [{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"id":"text"},
"childNodeCount": 1,
"localName": "span",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}]),
("css", "#text", [{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"id":"text"},
"childNodeCount": 1,
"localName": "span",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}]),
("xpath", "//p", [{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"data-class":"one"},
"childNodeCount": 1,
"localName": "p",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
},
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"data-class":"two"},
"childNodeCount": 1,
"localName": "p",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}]),
("innerText", "foo", [{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"data-class":"one"},
"childNodeCount": 1,
"localName": "p",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
},
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"data-class":"two"},
"childNodeCount": 1,
"localName": "p",
"namespaceURI": "http://www.w3.org/1999/xhtml",
"nodeType": 1,
}
}])
])
@pytest.mark.asyncio
async def test_locate_with_context_nodes(bidi_session, inline, top_context, type, value, expected):
url = inline("""<div id="parent">
<p data-class="one">foo</p>
<p data-class="two">foo</p>
<a data-class="three">
<span id="text">bar</span>
</a>
</div>""")
await bidi_session.browsing_context.navigate(
context=top_context["context"], url=url, wait="complete"
)
context_nodes = await bidi_session.script.evaluate(
expression="""document.querySelector("div")""",
target=ContextTarget(top_context["context"]),
await_promise=True,
)
result = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={ "type": type, "value": value },
start_nodes=[context_nodes]
)
recursive_compare(expected, result["nodes"])