From 8708586956303d8e224e5f20b3b155a776fdc14c Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Wed, 18 Dec 2024 11:57:41 +0100 Subject: [PATCH 1/3] [wdspec] change test_..._closes_browsing_context The tests relied on the script closing browsing context which causes "Scripts may only close windows that were opened by a script". The new approach is to close browsing context by a command during actions chain. This approach can be racy though. --- .../tests/bidi/input/perform_actions/key.py | 23 +++++++++++-------- .../input/perform_actions/pointer_mouse.py | 19 ++++++++++----- .../bidi/input/perform_actions/pointer_pen.py | 19 ++++++++++----- .../input/perform_actions/pointer_touch.py | 19 ++++++++++----- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/webdriver/tests/bidi/input/perform_actions/key.py b/webdriver/tests/bidi/input/perform_actions/key.py index 7deb5f1fb9900b..730e42eed273f4 100644 --- a/webdriver/tests/bidi/input/perform_actions/key.py +++ b/webdriver/tests/bidi/input/perform_actions/key.py @@ -19,13 +19,13 @@ async def test_invalid_browsing_context(bidi_session): await bidi_session.input.perform_actions(actions=actions, context="foo") -async def test_key_down_closes_browsing_context( - bidi_session, configuration, new_tab, inline -): - url = inline(""" - close - - """) +async def test_browsing_context_closed_before_key_up(bidi_session, + configuration, new_tab, get_test_page): + """ + If the browsing context is closed during the action chain, the action + command should fail with `NoSuchFrame` error code. + """ + url = get_test_page() await bidi_session.browsing_context.navigate( context=new_tab["context"], @@ -41,10 +41,13 @@ async def test_key_down_closes_browsing_context( .key_up("w") ) + actions_command_future = bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"]) + + await bidi_session.browsing_context.close(context=new_tab["context"]) + with pytest.raises(NoSuchFrameException): - await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] - ) + await actions_command_future async def test_key_backspace(bidi_session, top_context, setup_key_test): diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py index ab861a90937fad..7e3439fd1dd274 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py @@ -17,10 +17,14 @@ pytestmark = pytest.mark.asyncio -async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline +async def test_browsing_context_closed_before_pointer_up( + bidi_session, configuration, get_element, new_tab, get_test_page ): - url = inline("""close""") + """ + If the browsing context is closed during the action chain, the action + command should fail with `NoSuchFrame` error code. + """ + url = get_test_page() await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -39,10 +43,13 @@ async def test_pointer_down_closes_browsing_context( .pointer_up(button=0) ) + actions_command_future = bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"]) + + await bidi_session.browsing_context.close(context=new_tab["context"]) + with pytest.raises(NoSuchFrameException): - await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] - ) + await actions_command_future async def test_click_at_coordinates(bidi_session, top_context, load_static_test_page): diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py index cfc6beaeb4b579..74769696ed7e30 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py @@ -14,10 +14,14 @@ pytestmark = pytest.mark.asyncio -async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline +async def test_browsing_context_closed_before_pointer_up( + bidi_session, configuration, get_element, new_tab, get_test_page ): - url = inline("""close""") + """ + If the browsing context is closed during the action chain, the action + command should fail with `NoSuchFrame` error code. + """ + url = get_test_page() await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -36,10 +40,13 @@ async def test_pointer_down_closes_browsing_context( .pointer_up(button=0) ) + actions_command_future = bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"]) + + await bidi_session.browsing_context.close(context=new_tab["context"]) + with pytest.raises(NoSuchFrameException): - await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] - ) + await actions_command_future @pytest.mark.parametrize("origin", ["element", "pointer", "viewport"]) diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py index 4477162b322cbf..172922c0a43f9b 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py @@ -14,10 +14,14 @@ pytestmark = pytest.mark.asyncio -async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline +async def test_browsing_context_closed_before_pointer_up( + bidi_session, configuration, get_element, new_tab, get_test_page ): - url = inline("""close""") + """ + If the browsing context is closed during the action chain, the action + command should fail with `NoSuchFrame` error code. + """ + url = get_test_page() await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -36,10 +40,13 @@ async def test_pointer_down_closes_browsing_context( .pointer_up(button=0) ) + actions_command_future = bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"]) + + await bidi_session.browsing_context.close(context=new_tab["context"]) + with pytest.raises(NoSuchFrameException): - await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] - ) + await actions_command_future @pytest.mark.parametrize("origin", ["element", "pointer", "viewport"]) From 3fc84e8ddf32ed901fa4e9aa20e63ffa5b852cc6 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Wed, 18 Dec 2024 13:44:51 +0100 Subject: [PATCH 2/3] Revert "[wdspec] change test_..._closes_browsing_context" This reverts commit 8708586956303d8e224e5f20b3b155a776fdc14c. --- .../tests/bidi/input/perform_actions/key.py | 23 ++++++++----------- .../input/perform_actions/pointer_mouse.py | 19 +++++---------- .../bidi/input/perform_actions/pointer_pen.py | 19 +++++---------- .../input/perform_actions/pointer_touch.py | 19 +++++---------- 4 files changed, 28 insertions(+), 52 deletions(-) diff --git a/webdriver/tests/bidi/input/perform_actions/key.py b/webdriver/tests/bidi/input/perform_actions/key.py index 730e42eed273f4..7deb5f1fb9900b 100644 --- a/webdriver/tests/bidi/input/perform_actions/key.py +++ b/webdriver/tests/bidi/input/perform_actions/key.py @@ -19,13 +19,13 @@ async def test_invalid_browsing_context(bidi_session): await bidi_session.input.perform_actions(actions=actions, context="foo") -async def test_browsing_context_closed_before_key_up(bidi_session, - configuration, new_tab, get_test_page): - """ - If the browsing context is closed during the action chain, the action - command should fail with `NoSuchFrame` error code. - """ - url = get_test_page() +async def test_key_down_closes_browsing_context( + bidi_session, configuration, new_tab, inline +): + url = inline(""" + close + + """) await bidi_session.browsing_context.navigate( context=new_tab["context"], @@ -41,13 +41,10 @@ async def test_browsing_context_closed_before_key_up(bidi_session, .key_up("w") ) - actions_command_future = bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"]) - - await bidi_session.browsing_context.close(context=new_tab["context"]) - with pytest.raises(NoSuchFrameException): - await actions_command_future + await bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"] + ) async def test_key_backspace(bidi_session, top_context, setup_key_test): diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py index 7e3439fd1dd274..ab861a90937fad 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py @@ -17,14 +17,10 @@ pytestmark = pytest.mark.asyncio -async def test_browsing_context_closed_before_pointer_up( - bidi_session, configuration, get_element, new_tab, get_test_page +async def test_pointer_down_closes_browsing_context( + bidi_session, configuration, get_element, new_tab, inline ): - """ - If the browsing context is closed during the action chain, the action - command should fail with `NoSuchFrame` error code. - """ - url = get_test_page() + url = inline("""close""") await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -43,13 +39,10 @@ async def test_browsing_context_closed_before_pointer_up( .pointer_up(button=0) ) - actions_command_future = bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"]) - - await bidi_session.browsing_context.close(context=new_tab["context"]) - with pytest.raises(NoSuchFrameException): - await actions_command_future + await bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"] + ) async def test_click_at_coordinates(bidi_session, top_context, load_static_test_page): diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py index 74769696ed7e30..cfc6beaeb4b579 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py @@ -14,14 +14,10 @@ pytestmark = pytest.mark.asyncio -async def test_browsing_context_closed_before_pointer_up( - bidi_session, configuration, get_element, new_tab, get_test_page +async def test_pointer_down_closes_browsing_context( + bidi_session, configuration, get_element, new_tab, inline ): - """ - If the browsing context is closed during the action chain, the action - command should fail with `NoSuchFrame` error code. - """ - url = get_test_page() + url = inline("""close""") await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -40,13 +36,10 @@ async def test_browsing_context_closed_before_pointer_up( .pointer_up(button=0) ) - actions_command_future = bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"]) - - await bidi_session.browsing_context.close(context=new_tab["context"]) - with pytest.raises(NoSuchFrameException): - await actions_command_future + await bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"] + ) @pytest.mark.parametrize("origin", ["element", "pointer", "viewport"]) diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py index 172922c0a43f9b..4477162b322cbf 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py @@ -14,14 +14,10 @@ pytestmark = pytest.mark.asyncio -async def test_browsing_context_closed_before_pointer_up( - bidi_session, configuration, get_element, new_tab, get_test_page +async def test_pointer_down_closes_browsing_context( + bidi_session, configuration, get_element, new_tab, inline ): - """ - If the browsing context is closed during the action chain, the action - command should fail with `NoSuchFrame` error code. - """ - url = get_test_page() + url = inline("""close""") await bidi_session.browsing_context.navigate( context=new_tab["context"], url=url, @@ -40,13 +36,10 @@ async def test_browsing_context_closed_before_pointer_up( .pointer_up(button=0) ) - actions_command_future = bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"]) - - await bidi_session.browsing_context.close(context=new_tab["context"]) - with pytest.raises(NoSuchFrameException): - await actions_command_future + await bidi_session.input.perform_actions( + actions=actions, context=new_tab["context"] + ) @pytest.mark.parametrize("origin", ["element", "pointer", "viewport"]) From 6e9f1002577e04bfe23fd2da4ec47742e63c6550 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Wed, 18 Dec 2024 14:08:29 +0100 Subject: [PATCH 3/3] [wdspec] change test_..._closes_browsing_context Open context with `window.open` to allow it to be closed by script. --- .../tests/bidi/input/perform_actions/key.py | 22 +++++++++++----- .../input/perform_actions/pointer_mouse.py | 26 ++++++++++++++----- .../bidi/input/perform_actions/pointer_pen.py | 26 ++++++++++++++----- .../input/perform_actions/pointer_touch.py | 26 ++++++++++++++----- 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/webdriver/tests/bidi/input/perform_actions/key.py b/webdriver/tests/bidi/input/perform_actions/key.py index 7deb5f1fb9900b..567400c4e89957 100644 --- a/webdriver/tests/bidi/input/perform_actions/key.py +++ b/webdriver/tests/bidi/input/perform_actions/key.py @@ -10,6 +10,8 @@ pytestmark = pytest.mark.asyncio +CONTEXT_LOAD_EVENT = "browsingContext.load" + async def test_invalid_browsing_context(bidi_session): actions = Actions() @@ -20,18 +22,26 @@ async def test_invalid_browsing_context(bidi_session): async def test_key_down_closes_browsing_context( - bidi_session, configuration, new_tab, inline + bidi_session, configuration, new_tab, inline, subscribe_events, + wait_for_event ): url = inline(""" close """) - await bidi_session.browsing_context.navigate( - context=new_tab["context"], - url=url, - wait="complete", + # Opening a new context via `window.open` is required for script to be able + # to close it. + await subscribe_events(events=[CONTEXT_LOAD_EVENT]) + on_load = wait_for_event(CONTEXT_LOAD_EVENT) + + await bidi_session.script.evaluate( + expression=f"window.open('{url}')", + target=ContextTarget(new_tab["context"]), + await_promise=True ) + # Wait for the new context to be created and get it. + new_context = await on_load actions = Actions() ( @@ -43,7 +53,7 @@ async def test_key_down_closes_browsing_context( with pytest.raises(NoSuchFrameException): await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] + actions=actions, context=new_context["context"] ) diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py index ab861a90937fad..3e10b6698cf1ec 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_mouse.py @@ -2,6 +2,7 @@ from webdriver.bidi.error import MoveTargetOutOfBoundsException, NoSuchFrameException from webdriver.bidi.modules.input import Actions, get_element_origin +from webdriver.bidi.modules.script import ContextTarget from tests.support.asserts import assert_move_to_coordinates from tests.support.helpers import filter_dict @@ -16,18 +17,29 @@ pytestmark = pytest.mark.asyncio +CONTEXT_LOAD_EVENT = "browsingContext.load" + async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline + bidi_session, configuration, get_element, new_tab, inline, subscribe_events, + wait_for_event ): url = inline("""close""") - await bidi_session.browsing_context.navigate( - context=new_tab["context"], - url=url, - wait="complete", + + # Opening a new context via `window.open` is required for script to be able + # to close it. + await subscribe_events(events=[CONTEXT_LOAD_EVENT]) + on_load = wait_for_event(CONTEXT_LOAD_EVENT) + + await bidi_session.script.evaluate( + expression=f"window.open('{url}')", + target=ContextTarget(new_tab["context"]), + await_promise=True ) + # Wait for the new context to be created and get it. + new_context = await on_load - element = await get_element("input", context=new_tab) + element = await get_element("input", context=new_context) origin = get_element_origin(element) actions = Actions() @@ -41,7 +53,7 @@ async def test_pointer_down_closes_browsing_context( with pytest.raises(NoSuchFrameException): await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] + actions=actions, context=new_context["context"] ) diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py index cfc6beaeb4b579..f79223d61a5791 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_pen.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_pen.py @@ -2,6 +2,7 @@ from webdriver.bidi.error import MoveTargetOutOfBoundsException, NoSuchFrameException from webdriver.bidi.modules.input import Actions, get_element_origin +from webdriver.bidi.modules.script import ContextTarget from .. import get_events from . import ( @@ -13,18 +14,29 @@ pytestmark = pytest.mark.asyncio +CONTEXT_LOAD_EVENT = "browsingContext.load" + async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline + bidi_session, configuration, get_element, new_tab, inline, subscribe_events, + wait_for_event ): url = inline("""close""") - await bidi_session.browsing_context.navigate( - context=new_tab["context"], - url=url, - wait="complete", + + # Opening a new context via `window.open` is required for script to be able + # to close it. + await subscribe_events(events=[CONTEXT_LOAD_EVENT]) + on_load = wait_for_event(CONTEXT_LOAD_EVENT) + + await bidi_session.script.evaluate( + expression=f"window.open('{url}')", + target=ContextTarget(new_tab["context"]), + await_promise=True ) + # Wait for the new context to be created and get it. + new_context = await on_load - element = await get_element("input", context=new_tab) + element = await get_element("input", context=new_context) origin = get_element_origin(element) actions = Actions() @@ -38,7 +50,7 @@ async def test_pointer_down_closes_browsing_context( with pytest.raises(NoSuchFrameException): await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] + actions=actions, context=new_context["context"] ) diff --git a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py index 4477162b322cbf..79ff52a06b8b6f 100644 --- a/webdriver/tests/bidi/input/perform_actions/pointer_touch.py +++ b/webdriver/tests/bidi/input/perform_actions/pointer_touch.py @@ -2,6 +2,7 @@ from webdriver.bidi.error import MoveTargetOutOfBoundsException, NoSuchFrameException from webdriver.bidi.modules.input import Actions, get_element_origin +from webdriver.bidi.modules.script import ContextTarget from .. import get_events from . import ( @@ -13,18 +14,29 @@ pytestmark = pytest.mark.asyncio +CONTEXT_LOAD_EVENT = "browsingContext.load" + async def test_pointer_down_closes_browsing_context( - bidi_session, configuration, get_element, new_tab, inline + bidi_session, configuration, get_element, new_tab, inline, subscribe_events, + wait_for_event ): url = inline("""close""") - await bidi_session.browsing_context.navigate( - context=new_tab["context"], - url=url, - wait="complete", + + # Opening a new context via `window.open` is required for script to be able + # to close it. + await subscribe_events(events=[CONTEXT_LOAD_EVENT]) + on_load = wait_for_event(CONTEXT_LOAD_EVENT) + + await bidi_session.script.evaluate( + expression=f"window.open('{url}')", + target=ContextTarget(new_tab["context"]), + await_promise=True ) + # Wait for the new context to be created and get it. + new_context = await on_load - element = await get_element("input", context=new_tab) + element = await get_element("input", context=new_context) origin = get_element_origin(element) actions = Actions() @@ -38,7 +50,7 @@ async def test_pointer_down_closes_browsing_context( with pytest.raises(NoSuchFrameException): await bidi_session.input.perform_actions( - actions=actions, context=new_tab["context"] + actions=actions, context=new_context["context"] )