Skip to content

Commit 196ec5a

Browse files
committed
suggested changes
1 parent 66d8051 commit 196ec5a

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

py/selenium/webdriver/common/bidi/browsing_context.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import warnings
1819
from typing import Any, Callable, Optional, Union
1920

2021
from selenium.webdriver.common.bidi.common import command_builder
@@ -110,7 +111,12 @@ def from_json(cls, json: dict) -> "BrowsingContextInfo":
110111
children = None
111112
raw_children = json.get("children")
112113
if raw_children is not None and isinstance(raw_children, list):
113-
children = [BrowsingContextInfo.from_json(child) for child in raw_children if isinstance(child, dict)]
114+
children = []
115+
for child in raw_children:
116+
if isinstance(child, dict):
117+
children.append(BrowsingContextInfo.from_json(child))
118+
else:
119+
warnings.warn(f"Unexpected child type in browsing context: {type(child)}")
114120

115121
return cls(
116122
context=str(json.get("context", "")),
@@ -187,12 +193,32 @@ def from_json(cls, json: dict) -> "UserPromptOpenedParams":
187193
-------
188194
UserPromptOpenedParams: A new instance of UserPromptOpenedParams.
189195
"""
196+
context = json.get("context")
197+
if context is None or not isinstance(context, str):
198+
raise ValueError("context is required and must be a string")
199+
200+
handler = json.get("handler")
201+
if handler is None or not isinstance(handler, str):
202+
raise ValueError("handler is required and must be a string")
203+
204+
message = json.get("message")
205+
if message is None or not isinstance(message, str):
206+
raise ValueError("message is required and must be a string")
207+
208+
type_value = json.get("type")
209+
if type_value is None or not isinstance(type_value, str):
210+
raise ValueError("type is required and must be a string")
211+
212+
default_value = json.get("defaultValue")
213+
if default_value is not None and not isinstance(default_value, str):
214+
raise ValueError("defaultValue must be a string if provided")
215+
190216
return cls(
191-
context=str(json.get("context", "")),
192-
handler=str(json.get("handler", "")),
193-
message=str(json.get("message", "")),
194-
type=str(json.get("type", "")),
195-
default_value=str(json.get("defaultValue", "")),
217+
context=context,
218+
handler=handler,
219+
message=message,
220+
type=type_value,
221+
default_value=default_value,
196222
)
197223

198224

@@ -714,13 +740,15 @@ def remove_event_handler(self, event: str, callback_id: int) -> None:
714740
event_obj = BrowsingContextEvent(event_name)
715741

716742
self.conn.remove_callback(event_obj, callback_id)
717-
if event_name in self.subscriptions and callback_id in self.subscriptions[event_name]:
718-
self.subscriptions[event_name].remove(callback_id)
719-
if len(self.subscriptions[event_name]) == 0:
720-
params = {"events": [event_name]}
721-
session = Session(self.conn)
722-
self.conn.execute(session.unsubscribe(**params))
723-
del self.subscriptions[event_name]
743+
if event_name in self.subscriptions:
744+
callbacks = self.subscriptions[event_name]
745+
if callback_id in callbacks:
746+
callbacks.remove(callback_id)
747+
if not callbacks:
748+
params = {"events": [event_name]}
749+
session = Session(self.conn)
750+
self.conn.execute(session.unsubscribe(**params))
751+
del self.subscriptions[event_name]
724752

725753
def clear_event_handlers(self) -> None:
726754
"""Clear all event handlers from the browsing context."""

0 commit comments

Comments
 (0)