Skip to content
34 changes: 21 additions & 13 deletions py/selenium/webdriver/common/bidi/cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class CdpConnectionClosed(WsConnectionClosed):
def __init__(self, reason):
"""Constructor.

:param reason:
:type reason: wsproto.frame_protocol.CloseReason
Args:
reason: wsproto.frame_protocol.CloseReason
"""
self.reason = reason

Expand Down Expand Up @@ -205,8 +205,11 @@ def __init__(self, ws, session_id, target_id):
async def execute(self, cmd: Generator[dict, T, Any]) -> T:
"""Execute a command on the server and wait for the result.

:param cmd: any CDP command
:returns: a CDP result
Args:
cmd: any CDP command

Returns:
a CDP result
"""
cmd_id = next(self.id_iter)
cmd_event = trio.Event()
Expand Down Expand Up @@ -261,18 +264,20 @@ async def wait_for(self, event_type: type[T], buffer_size=10) -> AsyncGenerator[
def _handle_data(self, data):
"""Handle incoming WebSocket data.

:param dict data: a JSON dictionary
Args:
data: a JSON dictionary
"""
if "id" in data:
self._handle_cmd_response(data)
else:
self._handle_event(data)

def _handle_cmd_response(self, data):
def _handle_cmd_response(self, data: dict):
"""Handle a response to a command. This will set an event flag that
will return control to the task that called the command.

:param dict data: response as a JSON dictionary
Args:
data: response as a JSON dictionary
"""
cmd_id = data["id"]
try:
Expand All @@ -295,10 +300,11 @@ def _handle_cmd_response(self, data):
self.inflight_result[cmd_id] = return_
event.set()

def _handle_event(self, data):
def _handle_event(self, data: dict):
"""Handle an event.

:param dict data: event as a JSON dictionary
Args:
data: event as a JSON dictionary
"""
global devtools
event = devtools.util.parse_json_event(data)
Expand All @@ -325,9 +331,10 @@ class CdpSession(CdpBase):
def __init__(self, ws, session_id, target_id):
"""Constructor.

:param trio_websocket.WebSocketConnection ws:
:param devtools.target.SessionID session_id:
:param devtools.target.TargetID target_id:
Args:
ws: trio_websocket.WebSocketConnection
session_id: devtools.target.SessionID
target_id: devtools.target.TargetID
"""
super().__init__(ws, session_id, target_id)

Expand Down Expand Up @@ -394,7 +401,8 @@ class corresponds to the "root" session, i.e. the implicitly created
def __init__(self, ws):
"""Constructor.

:param trio_websocket.WebSocketConnection ws:
Args:
ws: trio_websocket.WebSocketConnection
"""
super().__init__(ws, session_id=None, target_id=None)
self.sessions = {}
Expand Down
59 changes: 15 additions & 44 deletions py/selenium/webdriver/common/bidi/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ class RealmInfo:
def from_json(cls, json: dict[str, Any]) -> "RealmInfo":
"""Creates a RealmInfo instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the realm information.

Returns:
-------
RealmInfo: A new instance of RealmInfo.
"""
if "realm" not in json:
Expand Down Expand Up @@ -95,12 +93,10 @@ class Source:
def from_json(cls, json: dict[str, Any]) -> "Source":
"""Creates a Source instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the source information.

Returns:
-------
Source: A new instance of Source.
"""
if "realm" not in json:
Expand All @@ -125,12 +121,10 @@ class EvaluateResult:
def from_json(cls, json: dict[str, Any]) -> "EvaluateResult":
"""Creates an EvaluateResult instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the evaluation result.

Returns:
-------
EvaluateResult: A new instance of EvaluateResult.
"""
if "realm" not in json:
Expand Down Expand Up @@ -160,12 +154,10 @@ def __init__(self, channel: str, data: dict, source: Source):
def from_json(cls, json: dict[str, Any]) -> "ScriptMessage":
"""Creates a ScriptMessage instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the script message.

Returns:
-------
ScriptMessage: A new instance of ScriptMessage.
"""
if "channel" not in json:
Expand Down Expand Up @@ -194,12 +186,10 @@ def __init__(self, realm_info: RealmInfo):
def from_json(cls, json: dict[str, Any]) -> "RealmCreated":
"""Creates a RealmCreated instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the realm created event.

Returns:
-------
RealmCreated: A new instance of RealmCreated.
"""
return cls(realm_info=RealmInfo.from_json(json))
Expand All @@ -217,12 +207,10 @@ def __init__(self, realm: str):
def from_json(cls, json: dict[str, Any]) -> "RealmDestroyed":
"""Creates a RealmDestroyed instance from a dictionary.

Parameters:
-----------
Args:
json: A dictionary containing the realm destroyed event.

Returns:
-------
RealmDestroyed: A new instance of RealmDestroyed.
"""
if "realm" not in json:
Expand Down Expand Up @@ -266,39 +254,33 @@ def remove_console_message_handler(self, id):
def pin(self, script: str) -> str:
"""Pins a script to the current browsing context.

Parameters:
-----------
Args:
script: The script to pin.

Returns:
-------
str: The ID of the pinned script.
"""
return self._add_preload_script(script)

def unpin(self, script_id: str) -> None:
"""Unpins a script from the current browsing context.

Parameters:
-----------
Args:
script_id: The ID of the pinned script to unpin.
"""
self._remove_preload_script(script_id)

def execute(self, script: str, *args) -> dict:
"""Executes a script in the current browsing context.

Parameters:
-----------
Args:
script: The script function to execute.
*args: Arguments to pass to the script function.

Returns:
-------
dict: The result value from the script execution.

Raises:
------
WebDriverException: If the script execution fails.
"""

Expand Down Expand Up @@ -390,20 +372,17 @@ def _add_preload_script(
) -> str:
"""Adds a preload script.

Parameters:
-----------
Args:
function_declaration: The function declaration to preload.
arguments: The arguments to pass to the function.
contexts: The browsing context IDs to apply the script to.
user_contexts: The user context IDs to apply the script to.
sandbox: The sandbox name to apply the script to.

Returns:
-------
str: The preload script ID.

Raises:
------
ValueError: If both contexts and user_contexts are provided.
"""
if contexts is not None and user_contexts is not None:
Expand All @@ -426,8 +405,7 @@ def _add_preload_script(
def _remove_preload_script(self, script_id: str) -> None:
"""Removes a preload script.

Parameters:
-----------
Args:
script_id: The preload script ID to remove.
"""
params = {"script": script_id}
Expand All @@ -436,8 +414,7 @@ def _remove_preload_script(self, script_id: str) -> None:
def _disown(self, handles: list[str], target: dict) -> None:
"""Disowns the given handles.

Parameters:
-----------
Args:
handles: The handles to disown.
target: The target realm or context.
"""
Expand All @@ -460,8 +437,7 @@ def _call_function(
) -> EvaluateResult:
"""Calls a provided function with given arguments in a given realm.

Parameters:
-----------
Args:
function_declaration: The function declaration to call.
await_promise: Whether to await promise resolution.
target: The target realm or context.
Expand All @@ -472,7 +448,6 @@ def _call_function(
user_activation: Whether to trigger user activation.

Returns:
-------
EvaluateResult: The result of the function call.
"""
params = {
Expand Down Expand Up @@ -505,8 +480,7 @@ def _evaluate(
) -> EvaluateResult:
"""Evaluates a provided script in a given realm.

Parameters:
-----------
Args:
expression: The script expression to evaluate.
target: The target realm or context.
await_promise: Whether to await promise resolution.
Expand All @@ -515,7 +489,6 @@ def _evaluate(
user_activation: Whether to trigger user activation.

Returns:
-------
EvaluateResult: The result of the script evaluation.
"""
params = {
Expand All @@ -540,13 +513,11 @@ def _get_realms(
) -> list[RealmInfo]:
"""Returns a list of all realms, optionally filtered.

Parameters:
-----------
Args:
context: The browsing context ID to filter by.
type: The realm type to filter by.

Returns:
-------
List[RealmInfo]: A list of realm information.
"""
params = {}
Expand Down
5 changes: 3 additions & 2 deletions py/selenium/webdriver/common/driver_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class DriverFinder:
"""A Driver finding class responsible for obtaining the correct driver and
associated browser.
:param service: instance of the driver service class.
:param options: instance of the browser options class.
Args:
service: instance of the driver service class.
options: instance of the browser options class.
"""

def __init__(self, service: Service, options: BaseOptions) -> None:
Expand Down
Loading