|
| 1 | +import pytest |
| 2 | +import asyncio |
| 3 | + |
| 4 | +from webdriver.bidi.modules.script import ContextTarget |
| 5 | + |
| 6 | +pytestmark = pytest.mark.asyncio |
| 7 | + |
| 8 | +CONTEXT_DESTROYED_EVENT = "browsingContext.contextDestroyed" |
| 9 | +USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened" |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("type_hint", ["window", "tab"]) |
| 13 | +@pytest.mark.parametrize("prompt_unload", [None, False]) |
| 14 | +async def test_prompt_unload_not_triggering_dialog( |
| 15 | + bidi_session, |
| 16 | + subscribe_events, |
| 17 | + setup_beforeunload_page, |
| 18 | + wait_for_event, |
| 19 | + wait_for_future_safe, |
| 20 | + type_hint, |
| 21 | + prompt_unload, |
| 22 | +): |
| 23 | + |
| 24 | + new_context = await bidi_session.browsing_context.create(type_hint=type_hint) |
| 25 | + |
| 26 | + # Set up event listener to make sure the "beforeunload" event is not emitted |
| 27 | + await subscribe_events([USER_PROMPT_OPENED_EVENT, CONTEXT_DESTROYED_EVENT]) |
| 28 | + # Track all received browsingContext.userPromptOpened events in the events array |
| 29 | + events = [] |
| 30 | + |
| 31 | + async def on_event(method, data): |
| 32 | + if method == USER_PROMPT_OPENED_EVENT: |
| 33 | + events.append(data) |
| 34 | + |
| 35 | + remove_listener = bidi_session.add_event_listener( |
| 36 | + USER_PROMPT_OPENED_EVENT, on_event |
| 37 | + ) |
| 38 | + |
| 39 | + await setup_beforeunload_page(new_context) |
| 40 | + |
| 41 | + on_context_destroyed = wait_for_event(CONTEXT_DESTROYED_EVENT) |
| 42 | + |
| 43 | + await bidi_session.browsing_context.close( |
| 44 | + context=new_context["context"], prompt_unload=prompt_unload |
| 45 | + ) |
| 46 | + |
| 47 | + await wait_for_future_safe(on_context_destroyed) |
| 48 | + |
| 49 | + assert events == [] |
| 50 | + |
| 51 | + remove_listener() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("type_hint", ["window", "tab"]) |
| 55 | +async def test_prompt_unload_triggering_dialog( |
| 56 | + bidi_session, |
| 57 | + setup_beforeunload_page, |
| 58 | + subscribe_events, |
| 59 | + wait_for_event, |
| 60 | + wait_for_future_safe, |
| 61 | + type_hint, |
| 62 | +): |
| 63 | + |
| 64 | + new_context = await bidi_session.browsing_context.create(type_hint=type_hint) |
| 65 | + |
| 66 | + # Set up event listener to make sure the "beforeunload" event is not emitted |
| 67 | + await subscribe_events([USER_PROMPT_OPENED_EVENT, CONTEXT_DESTROYED_EVENT]) |
| 68 | + user_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT) |
| 69 | + |
| 70 | + # Track all received browsingContext.contextDestroyed events in the events array |
| 71 | + events = [] |
| 72 | + |
| 73 | + async def on_event(_, data): |
| 74 | + if data["type"] == CONTEXT_DESTROYED_EVENT: |
| 75 | + events.append(data) |
| 76 | + |
| 77 | + remove_listener = bidi_session.add_event_listener( |
| 78 | + CONTEXT_DESTROYED_EVENT, on_event) |
| 79 | + |
| 80 | + await setup_beforeunload_page(new_context) |
| 81 | + |
| 82 | + close_task = asyncio.create_task( |
| 83 | + bidi_session.browsing_context.close( |
| 84 | + context=new_context["context"], prompt_unload=True |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + await wait_for_future_safe(user_prompt_opened) |
| 89 | + |
| 90 | + # Events that come after the handling are OK |
| 91 | + remove_listener() |
| 92 | + assert events == [] |
| 93 | + |
| 94 | + await bidi_session.browsing_context.handle_user_prompt( |
| 95 | + context=new_context["context"], |
| 96 | + ) |
| 97 | + |
| 98 | + await close_task |
| 99 | + |
| 100 | + contexts = await bidi_session.browsing_context.get_tree() |
| 101 | + assert len(contexts) == 1 |
| 102 | + |
| 103 | + assert contexts[0]["context"] != new_context["context"] |
0 commit comments