|
| 1 | +import pytest |
| 2 | +from webdriver.bidi.modules.script import ContextTarget |
| 3 | + |
| 4 | +from tests.bidi import wait_for_bidi_events |
| 5 | + |
| 6 | + |
| 7 | +pytestmark = pytest.mark.asyncio |
| 8 | + |
| 9 | +CONTEXT_CREATED_EVENT = "browsingContext.contextCreated" |
| 10 | +REALM_CREATED_EVENT = "script.realmCreated" |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("window_url", ["", "about:blank", "inline"]) |
| 14 | +async def test_window_open( |
| 15 | + bidi_session, subscribe_events, top_context, inline, window_url |
| 16 | +): |
| 17 | + await subscribe_events(events=[REALM_CREATED_EVENT]) |
| 18 | + |
| 19 | + events = [] |
| 20 | + |
| 21 | + async def on_event(method, data): |
| 22 | + events.append(data) |
| 23 | + |
| 24 | + remove_listener = bidi_session.add_event_listener(REALM_CREATED_EVENT, on_event) |
| 25 | + |
| 26 | + if window_url == "inline": |
| 27 | + window_url = inline("<div>in window</div>") |
| 28 | + |
| 29 | + await bidi_session.script.evaluate( |
| 30 | + expression=f"window.open('{window_url}')", |
| 31 | + await_promise=False, |
| 32 | + target=ContextTarget(top_context["context"]), |
| 33 | + ) |
| 34 | + |
| 35 | + await wait_for_bidi_events(bidi_session, events, 1, equal_check=True) |
| 36 | + |
| 37 | + realms = await bidi_session.script.get_realms() |
| 38 | + window_realm = None |
| 39 | + for realm in realms: |
| 40 | + if realm["context"] != top_context["context"]: |
| 41 | + window_realm = realm |
| 42 | + |
| 43 | + assert events[-1] == window_realm |
| 44 | + |
| 45 | + remove_listener() |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.parametrize("window_url", ["", "about:blank", "inline"]) |
| 49 | +async def test_event_order( |
| 50 | + bidi_session, subscribe_events, new_tab, inline, window_url |
| 51 | +): |
| 52 | + await subscribe_events(events=[CONTEXT_CREATED_EVENT, REALM_CREATED_EVENT]) |
| 53 | + |
| 54 | + events = [] |
| 55 | + |
| 56 | + async def on_event(method, data): |
| 57 | + events.append(method) |
| 58 | + |
| 59 | + remove_listener_for_context_created = bidi_session.add_event_listener( |
| 60 | + CONTEXT_CREATED_EVENT, on_event |
| 61 | + ) |
| 62 | + remove_listener_for_realm_created = bidi_session.add_event_listener( |
| 63 | + REALM_CREATED_EVENT, on_event |
| 64 | + ) |
| 65 | + |
| 66 | + if window_url == "inline": |
| 67 | + window_url = inline("<div>in window</div>") |
| 68 | + |
| 69 | + events = [] |
| 70 | + |
| 71 | + await bidi_session.script.evaluate( |
| 72 | + expression=f"window.open('{window_url}')", |
| 73 | + await_promise=False, |
| 74 | + target=ContextTarget(new_tab["context"]), |
| 75 | + ) |
| 76 | + |
| 77 | + await wait_for_bidi_events(bidi_session, events, 2, equal_check=True) |
| 78 | + |
| 79 | + assert events[0] == CONTEXT_CREATED_EVENT |
| 80 | + assert events[1] == REALM_CREATED_EVENT |
| 81 | + |
| 82 | + remove_listener_for_context_created() |
| 83 | + remove_listener_for_realm_created() |
0 commit comments