Debian Codegraph @

testing/web-platform/mozilla/tests/webdriver/bidi/script/evaluate.py

raw
1 import asyncio
2
3 import pytest
4 from webdriver.bidi.modules.script import ContextTarget
5
6 pytestmark = pytest.mark.asyncio
7
8
9 DOCUMENT_LOADED_SCRIPT = """
10 new Promise(resolve => {
11 const checkDone = () => {
12 if (window.location.href !== "about:blank") {
13 resolve(window.location.href);
14 }
15 };
16
17 if (document.readyState === "complete") {
18 checkDone();
19 }
20 window.addEventListener("load", checkDone);
21 })
22 """
23
24 PAGE = "/webdriver/tests/support/html/default.html?pipe=trickle(d1)"
25
26
27 async def test_retry_during_initial_load(bidi_session, url, new_tab):
28 page_url = url(PAGE)
29
30 # Open a new window that triggers a cross-origin navigation to force a
31 # replacement of the initial browsing context for "about:blank"
32 result = await bidi_session.script.evaluate(
33 expression=f"""window.open("{page_url}")""",
34 await_promise=False,
35 target=ContextTarget(new_tab["context"]),
36 )
37 new_window = result["value"]
38
39 try:
40 # Check that the script's execution in the window modal is retried.
41 result = await bidi_session.script.evaluate(
42 expression=DOCUMENT_LOADED_SCRIPT,
43 await_promise=True,
44 target=ContextTarget(new_window["context"]),
45 )
46
47 assert result["value"] == page_url
48 finally:
49 await bidi_session.browsing_context.close(context=new_window["context"])
50
51
52 async def test_retry_during_navigation(bidi_session, new_tab, url):
53 page_url = url(PAGE)
54
55 # Start a new navigation that triggers a cross-origin navigation to force a
56 # replacement of the initial browsing context for "about:blank"
57 asyncio.create_task(
58 bidi_session.browsing_context.navigate(
59 context=new_tab["context"], url=page_url, wait="none"
60 )
61 )
62
63 # Execute a script that will only resolve when the navigation finished.
64 # Here we expect that the command will automatically be retried when the
65 # browsing context gets replaced.
66 result = await bidi_session.script.evaluate(
67 expression=DOCUMENT_LOADED_SCRIPT,
68 await_promise=True,
69 target=ContextTarget(new_tab["context"]),
70 )
71
72 assert result["value"] == page_url