From e971373aff0974a013ee131edd85e09c990632f4 Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Mon, 15 Apr 2024 12:09:53 +0200 Subject: [PATCH] Add types --- console/console-log-logged.html | 2 +- resources/testdriver.js | 7 ++++--- .../wptrunner/wptrunner/executors/asyncactions.py | 15 +++++---------- .../wptrunner/executors/executorwebdriver.py | 4 +--- tools/wptrunner/wptrunner/executors/protocol.py | 2 +- tools/wptrunner/wptrunner/testdriver-extra.js | 13 ++++++++++--- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/console/console-log-logged.html b/console/console-log-logged.html index 921481e5bcc6dd3..da811a15ec3ddcd 100644 --- a/console/console-log-logged.html +++ b/console/console-log-logged.html @@ -9,7 +9,7 @@ promise_test(async () => { const some_message = "SOME MESSAGE"; // Subscribe to `log.entryAdded` BiDi events. This will not add a listener to the page. - await test_driver.bidi.log.entry_added.subscribe({contexts: [window]}); + await test_driver.bidi.log.entry_added.subscribe(); // Add a listener for the log.entryAdded event. This will not subscribe to the event, so the subscription is // required before. The cleanup is done automatically after the test is finished. const log_entry_promise = test_driver.bidi.log.entry_added.once(); diff --git a/resources/testdriver.js b/resources/testdriver.js index 3d1044444e8a831..1dd5c756178d97e 100644 --- a/resources/testdriver.js +++ b/resources/testdriver.js @@ -63,9 +63,10 @@ /** * Subscribe to the `log.entryAdded` event. This does not add actual listeners. To listen to the * event, use `on` method. - * @param {{contexts?: (string | Window)[]}} props - Parameters for the subscription. - * `contexts` is an array of window proxies or browsing context ids to listen to the event. If not - * provided, the event subscription is global. + * @param {{contexts?: null | (string | Window)[]}} props - Parameters for the subscription. + * * `contexts`: an array of window proxies or browsing context ids to listen to the event. If not + * provided, the event subscription is done for the current window's browsing context. `null` for + * the global subscription. * @return {Promise} */ subscribe: async function (props = {}) { diff --git a/tools/wptrunner/wptrunner/executors/asyncactions.py b/tools/wptrunner/wptrunner/executors/asyncactions.py index 33e6d84967ad224..735aa017ce16111 100644 --- a/tools/wptrunner/wptrunner/executors/asyncactions.py +++ b/tools/wptrunner/wptrunner/executors/asyncactions.py @@ -1,23 +1,18 @@ # mypy: allow-untyped-defs import sys -if sys.version_info < (3, 11): - from typing_extensions import TypedDict, NotRequired -else: - from typing import TypedDict, NotRequired - from typing import Dict, List, Literal, Optional, Union # TODO: check if type annotation is supported by all the required versions of Python. # noinspection PyCompatibility -class WindowProxyProperties(TypedDict): +class WindowProxyProperties(Dict): context: str # TODO: check if type annotation is supported by all the required versions of Python. # noinspection PyCompatibility -class WindowProxyRemoteValue(TypedDict): +class WindowProxyRemoteValue(Dict): """ WebDriver BiDi browsing context descriptor. """ @@ -30,7 +25,7 @@ class BidiSessionSubscribeAction: # TODO: check if type annotation is supported by all the required versions of Python. # noinspection PyCompatibility - class Payload(TypedDict): + class Payload(Dict): """ Payload for the "bidi.session.subscribe" action. events: List of event names to subscribe to. @@ -38,7 +33,7 @@ class Payload(TypedDict): or a string. The latter is considered as a browsing context id. """ events: List[str] - contexts: NotRequired[Optional[List[Union[str, WindowProxyRemoteValue]]]] + contexts: Optional[List[Union[str, WindowProxyRemoteValue]]] def __init__(self, logger, protocol): self.logger = logger @@ -47,7 +42,7 @@ def __init__(self, logger, protocol): async def __call__(self, payload: Payload): events = payload["events"] contexts = None - if "contexts" in payload and payload["contexts"] is not None: + if payload["contexts"] is not None: contexts = [] for c in payload["contexts"]: if isinstance(c, str): diff --git a/tools/wptrunner/wptrunner/executors/executorwebdriver.py b/tools/wptrunner/wptrunner/executors/executorwebdriver.py index 04b87d1dbc27f27..f836173af347bdd 100644 --- a/tools/wptrunner/wptrunner/executors/executorwebdriver.py +++ b/tools/wptrunner/wptrunner/executors/executorwebdriver.py @@ -829,9 +829,7 @@ def _get_next_message(self, protocol, url, test_window): message = protocol.loop.run_until_complete(protocol.bidi_script.async_call_function( wrapped_script, context=test_window, args=[bidi_url_argument])) - - self.logger.debug("Received BiDi message from test_driver: %s" % message) - # The message is a BiDi action. Deserialize it. + # The message is in WebDriver BiDi format. Deserialize it. deserialized_message = self.bidi_deserialize(message) self.logger.debug("Deserialized message from test_driver: %s" % deserialized_message) return deserialized_message diff --git a/tools/wptrunner/wptrunner/executors/protocol.py b/tools/wptrunner/wptrunner/executors/protocol.py index a3f507c6dd32773..b045d349277bd22 100644 --- a/tools/wptrunner/wptrunner/executors/protocol.py +++ b/tools/wptrunner/wptrunner/executors/protocol.py @@ -328,7 +328,7 @@ class BidiEventsProtocolPart(ProtocolPart): name = "bidi_events" @abstractmethod - async def subscribe(self, events, contexts=None): + async def subscribe(self, events, contexts): """Subscribe to events. :param list events: The list of events names to subscribe to. diff --git a/tools/wptrunner/wptrunner/testdriver-extra.js b/tools/wptrunner/wptrunner/testdriver-extra.js index 3a19ec39bdaa2fc..045667147501b42 100644 --- a/tools/wptrunner/wptrunner/testdriver-extra.js +++ b/tools/wptrunner/wptrunner/testdriver-extra.js @@ -167,14 +167,21 @@ return pending_promise; }; - const subscribe = function(props) { - return create_action("bidi.session.subscribe", {...props}); + const subscribe = function (props) { + return create_action("bidi.session.subscribe", { + // Default to subscribing to the window's events. + contexts: [window], + ...props + }); }; window.test_driver_internal.in_automation = true; window.test_driver_internal.bidi.log.entry_added.subscribe = function (props) { - return subscribe({...(props ?? {}), events: ["log.entryAdded"]}) + return subscribe({ + ...(props ?? {}), + events: ["log.entryAdded"] + }) }; window.test_driver_internal.bidi.log.entry_added.on = function (callback) {