Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Apr 15, 2024
1 parent 492b967 commit 4631c31
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion console/console-log-logged.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
*/
subscribe: async function (props = {}) {
Expand Down
15 changes: 5 additions & 10 deletions tools/wptrunner/wptrunner/executors/asyncactions.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -30,15 +25,15 @@ 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.
contexts: Optional list of browsing contexts to subscribe to. Each context can be either a BiDi serialized value,
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
Expand All @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4631c31

Please sign in to comment.