From 68376e31d8cab7950a44d962af30ed5b22f8a4de Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 27 Dec 2023 10:32:26 +0100 Subject: [PATCH] [wdspec] Add tests for permissions --- tools/webdriver/webdriver/bidi/client.py | 1 + .../webdriver/bidi/modules/__init__.py | 1 + .../webdriver/bidi/modules/permissions.py | 28 ++++++++++ webdriver/tests/bidi/permissions/__init__.py | 0 .../permissions/set_permission/__init__.py | 23 ++++++++ .../permissions/set_permission/invalid.py | 53 +++++++++++++++++++ .../set_permission/set_permission.py | 42 +++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 tools/webdriver/webdriver/bidi/modules/permissions.py create mode 100644 webdriver/tests/bidi/permissions/__init__.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/__init__.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/invalid.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/set_permission.py diff --git a/tools/webdriver/webdriver/bidi/client.py b/tools/webdriver/webdriver/bidi/client.py index 8f891ffafd60d2a..73bba55791b3d46 100644 --- a/tools/webdriver/webdriver/bidi/client.py +++ b/tools/webdriver/webdriver/bidi/client.py @@ -94,6 +94,7 @@ def __init__(self, self.browsing_context = modules.BrowsingContext(self) self.input = modules.Input(self) self.network = modules.Network(self) + self.permissions = modules.Permissions(self) self.script = modules.Script(self) self.session = modules.Session(self) self.storage = modules.Storage(self) diff --git a/tools/webdriver/webdriver/bidi/modules/__init__.py b/tools/webdriver/webdriver/bidi/modules/__init__.py index 6f63e85bcd06b8a..0a2ef500c42ce0d 100644 --- a/tools/webdriver/webdriver/bidi/modules/__init__.py +++ b/tools/webdriver/webdriver/bidi/modules/__init__.py @@ -4,6 +4,7 @@ from .browsing_context import BrowsingContext from .input import Input from .network import Network +from .permissions import Permissions from .script import Script from .session import Session from .storage import Storage diff --git a/tools/webdriver/webdriver/bidi/modules/permissions.py b/tools/webdriver/webdriver/bidi/modules/permissions.py new file mode 100644 index 000000000000000..a914b3b7b28aceb --- /dev/null +++ b/tools/webdriver/webdriver/bidi/modules/permissions.py @@ -0,0 +1,28 @@ +from typing import Any, Optional, Mapping, MutableMapping + +from ._module import BidiModule, command + + +class Permissions(BidiModule): + @command + def end(self) -> Mapping[str, Any]: + return {} + + @end.result + async def _end(self, result: Mapping[str, Any]) -> Any: + if self.session.transport: + await self.session.transport.wait_closed() + + return result + + @command + def set_permission(self, + descriptor: Optional[Mapping[str, Any]] = None, + state: Optional[str] = None, + origin: Optional[str] = None) -> Mapping[str, Any]: + params: MutableMapping[str, Any] = { + "descriptor": descriptor, + "state": state, + "origin": origin + } + return params diff --git a/webdriver/tests/bidi/permissions/__init__.py b/webdriver/tests/bidi/permissions/__init__.py new file mode 100644 index 000000000000000..e69de29bb2d1d64 diff --git a/webdriver/tests/bidi/permissions/set_permission/__init__.py b/webdriver/tests/bidi/permissions/set_permission/__init__.py new file mode 100644 index 000000000000000..b8f6358d61746af --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/__init__.py @@ -0,0 +1,23 @@ +from typing import Any, Mapping + +from webdriver.bidi.modules.script import ContextTarget + +async def get_permission_state(bidi_session, context: Mapping[str, Any], name: str) -> str: + result = await bidi_session.script.call_function( + function_declaration="""() => { + return navigator.permissions.query({ name: '%s' }) + .then(val => val.state, err => err.message) + }""" % name, + target=ContextTarget(context["context"]), + await_promise=True) + return result["value"] + + +async def get_context_origin(bidi_session, context: Mapping[str, Any]) -> str: + result = await bidi_session.script.call_function( + function_declaration="""() => { + return window.location.origin; + }""", + target=ContextTarget(context["context"]), + await_promise=False) + return result["value"] diff --git a/webdriver/tests/bidi/permissions/set_permission/invalid.py b/webdriver/tests/bidi/permissions/set_permission/invalid.py new file mode 100644 index 000000000000000..e2263ae62814a39 --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/invalid.py @@ -0,0 +1,53 @@ +import pytest +import webdriver.bidi.error as error + +pytestmark = pytest.mark.asyncio + +@pytest.mark.parametrize("descriptor", [False, "SOME_STRING", 42, {}, [], {"name": 23}, None]) +async def test_params_descriptor_invalid_type(bidi_session, descriptor): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor=descriptor, + state="granted", + origin="https://example.com", + ) + + +@pytest.mark.parametrize("descriptor", [{"name": "unknown"}]) +async def test_params_descriptor_invalid_value(bidi_session, descriptor): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor=descriptor, + state="granted", + origin="https://example.com", + ) + + +@pytest.mark.parametrize("state", [False, 42, {}, [], None]) +async def test_params_state_invalid_type(bidi_session, state): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state=state, + origin="https://example.com", + ) + + +@pytest.mark.parametrize("state", ["UNKOWN", "Granted"]) +async def test_params_state_invalid_value(bidi_session, state): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state=state, + origin="https://example.com", + ) + + +@pytest.mark.parametrize("origin", [False, 42, {}, [], None]) +async def test_params_origin_invalid_type(bidi_session, origin): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) diff --git a/webdriver/tests/bidi/permissions/set_permission/set_permission.py b/webdriver/tests/bidi/permissions/set_permission/set_permission.py new file mode 100644 index 000000000000000..a32cb94625d183b --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/set_permission.py @@ -0,0 +1,42 @@ +import pytest + +from . import get_context_origin, get_permission_state + +pytestmark = pytest.mark.asyncio + +@pytest.mark.asyncio +async def test_set_permission(bidi_session, new_tab, url): + test_url = url("/common/blank.html", protocol="https") + await bidi_session.browsing_context.navigate( + context=new_tab["context"], + url=test_url, + wait="complete", + ) + + origin = await get_context_origin(bidi_session, new_tab) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "granted" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="denied", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "denied" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="prompt", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt"