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

[wdspec] add context locator tests involving iframes and shadow DOM #50047

Merged
merged 4 commits into from
Jan 17, 2025
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
60 changes: 59 additions & 1 deletion webdriver/tests/bidi/browsing_context/locate_nodes/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,51 @@ async def test_params_locator_accessability_value_invalid_type(
)


@pytest.mark.parametrize("value", [None, False, 42, "", []])
async def test_params_locator_context_invalid_type(bidi_session, inline,
top_context, value):
await navigate_to_page(bidi_session, inline, top_context)

with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "context", "value": value}
)


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_locator_context_value_invalid_type(bidi_session, inline,
top_context, value):
await navigate_to_page(bidi_session, inline, top_context)

with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "context", "value": {
"context": value
}}
)


@pytest.mark.parametrize("value", ["non_exiting_context"])
async def test_params_locator_context_value_invalid_context(bidi_session,
inline, top_context, value):
await navigate_to_page(bidi_session, inline, top_context)

with pytest.raises(error.NoSuchFrameException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "context", "value": {
"context": value
}}
)

@pytest.mark.parametrize("type,value", [
("css", "a*b"),
("xpath", ""),
("innerText", ""),
("accessibility", {}),
("context", {})
("context", {"context": ""})
sadym-chromium marked this conversation as resolved.
Show resolved Hide resolved
])
async def test_params_locator_value_invalid_value(bidi_session, inline, top_context, type, value):
await navigate_to_page(bidi_session, inline, top_context)
Expand Down Expand Up @@ -218,3 +257,22 @@ async def test_params_start_nodes_dom_node_not_element(
locator={"type": "css", "value": "div"},
start_nodes=[remote_reference],
)


@pytest.mark.asyncio
async def test_locate_by_context_invalid_context(bidi_session, inline, top_context, iframe):
iframe_url = inline(iframe("<div>foo</div>"))
page_url = inline(f"<iframe src='{iframe_url}'></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"])
iframe2_context = contexts[0]["children"][0]["children"][0]

with pytest.raises(error.InvalidArgumentException):
await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "context", "value": { "context": iframe2_context["context"] }}
)
88 changes: 85 additions & 3 deletions webdriver/tests/bidi/browsing_context/locate_nodes/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ async def test_locate_by_accessibility_attributes(
@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"])
@pytest.mark.asyncio
async def test_locate_by_context(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>")
iframe_url_1 = inline("<div>foo</div>", domain=domain)
page_url = inline(f"<iframe id='target' src='{iframe_url_1}'></iframe>")
sadym-chromium marked this conversation as resolved.
Show resolved Hide resolved

await bidi_session.browsing_context.navigate(
context=top_context["context"], url=page_url, wait="complete"
Expand All @@ -261,7 +261,89 @@ async def test_locate_by_context(bidi_session, inline, top_context, domain):
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"src": any_string},
"attributes": {"src": any_string, "id": "target"},
"childNodeCount": 0,
"localName": "iframe",
"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_by_context_in_iframe(bidi_session, inline, top_context, domain):
iframe_url_2 = inline("<div>foo</div>", domain=domain)
iframe_url_1 = inline(f"<div><iframe id='target' src='{iframe_url_2}'></iframe></div>")
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"])
iframe1_context = contexts[0]["children"][0]
iframe2_context = contexts[0]["children"][0]["children"][0]

result = await bidi_session.browsing_context.locate_nodes(
context=iframe1_context["context"],
locator={"type": "context", "value": { "context": iframe2_context["context"] }}
)

expected = [
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"src": any_string, "id": "target"},
"childNodeCount": 0,
"localName": "iframe",
"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.parametrize("mode", ["open", "closed"])
@pytest.mark.asyncio
async def test_locate_by_context_in_shadow_dom(bidi_session, inline, top_context, domain, mode):
iframe_url_1 = inline(f"<div>foo</div>", domain=domain)
sadym-chromium marked this conversation as resolved.
Show resolved Hide resolved
page_url = inline(f"""
<div id="host"></div>
<script>
const host = document.querySelector('#host');
const shadow = host.attachShadow({{ mode: '{mode}' }});
const div = document.createElement('div');
div.innerHTML = "<iframe id='target' src='{iframe_url_1}'></iframe>";
shadow.appendChild(div);
</script>
""")

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"])
iframe1_context = contexts[0]["children"][0]

result = await bidi_session.browsing_context.locate_nodes(
context=top_context["context"],
locator={"type": "context", "value": { "context": iframe1_context["context"] }}
)

expected = [
{
"type": "node",
"sharedId": any_string,
"value": {
"attributes": {"src": any_string, "id": "target"},
"childNodeCount": 0,
"localName": "iframe",
"namespaceURI": "http://www.w3.org/1999/xhtml",
Expand Down
Loading