Skip to content

Commit e7b47d2

Browse files
committed
add downloadEnd event
1 parent 55f02a9 commit e7b47d2

File tree

1 file changed

+101
-29
lines changed

1 file changed

+101
-29
lines changed

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

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -183,41 +183,17 @@ def __init__(
183183

184184
@classmethod
185185
def from_json(cls, json: dict) -> "DownloadWillBeginParams":
186-
"""Creates a DownloadWillBeginParams instance from a dictionary.
187-
188-
Parameters:
189-
-----------
190-
json: A dictionary containing the download parameters.
191-
192-
Returns:
193-
-------
194-
DownloadWillBeginParams: A new instance of DownloadWillBeginParams.
195-
"""
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-
navigation = json.get("navigation")
201-
if navigation is not None and not isinstance(navigation, str):
202-
raise ValueError("navigation must be a string")
203-
204-
timestamp = json.get("timestamp")
205-
if timestamp is None or not isinstance(timestamp, int) or timestamp < 0:
206-
raise ValueError("timestamp is required and must be a non-negative integer")
207-
208-
url = json.get("url")
209-
if url is None or not isinstance(url, str):
210-
raise ValueError("url is required and must be a string")
186+
nav_info = NavigationInfo.from_json(json)
211187

212188
suggested_filename = json.get("suggestedFilename")
213189
if suggested_filename is None or not isinstance(suggested_filename, str):
214190
raise ValueError("suggestedFilename is required and must be a string")
215191

216192
return cls(
217-
context=context,
218-
navigation=navigation,
219-
timestamp=timestamp,
220-
url=url,
193+
context=nav_info.context,
194+
navigation=nav_info.navigation,
195+
timestamp=nav_info.timestamp,
196+
url=nav_info.url,
221197
suggested_filename=suggested_filename,
222198
)
223199

@@ -375,6 +351,91 @@ def from_json(cls, json: dict) -> "HistoryUpdatedParams":
375351
)
376352

377353

354+
class DownloadCanceledParams(NavigationInfo):
355+
def __init__(
356+
self,
357+
context: str,
358+
navigation: Optional[str],
359+
timestamp: int,
360+
url: str,
361+
status: str = "canceled",
362+
):
363+
super().__init__(context, navigation, timestamp, url)
364+
self.status = status
365+
366+
@classmethod
367+
def from_json(cls, json: dict) -> "DownloadCanceledParams":
368+
nav_info = NavigationInfo.from_json(json)
369+
370+
status = json.get("status")
371+
if status is None or status != "canceled":
372+
raise ValueError("status is required and must be 'canceled'")
373+
374+
return cls(
375+
context=nav_info.context,
376+
navigation=nav_info.navigation,
377+
timestamp=nav_info.timestamp,
378+
url=nav_info.url,
379+
status=status,
380+
)
381+
382+
383+
class DownloadCompleteParams(NavigationInfo):
384+
def __init__(
385+
self,
386+
context: str,
387+
navigation: Optional[str],
388+
timestamp: int,
389+
url: str,
390+
status: str = "complete",
391+
filepath: Optional[str] = None,
392+
):
393+
super().__init__(context, navigation, timestamp, url)
394+
self.status = status
395+
self.filepath = filepath
396+
397+
@classmethod
398+
def from_json(cls, json: dict) -> "DownloadCompleteParams":
399+
nav_info = NavigationInfo.from_json(json)
400+
401+
status = json.get("status")
402+
if status is None or status != "complete":
403+
raise ValueError("status is required and must be 'complete'")
404+
405+
filepath = json.get("filepath")
406+
if filepath is not None and not isinstance(filepath, str):
407+
raise ValueError("filepath must be a string if provided")
408+
409+
return cls(
410+
context=nav_info.context,
411+
navigation=nav_info.navigation,
412+
timestamp=nav_info.timestamp,
413+
url=nav_info.url,
414+
status=status,
415+
filepath=filepath,
416+
)
417+
418+
419+
class DownloadEndParams:
420+
"""Parameters for the downloadEnd event."""
421+
422+
def __init__(
423+
self,
424+
download_params: Union[DownloadCanceledParams, DownloadCompleteParams],
425+
):
426+
self.download_params = download_params
427+
428+
@classmethod
429+
def from_json(cls, json: dict) -> "DownloadEndParams":
430+
status = json.get("status")
431+
if status == "canceled":
432+
return cls(DownloadCanceledParams.from_json(json))
433+
elif status == "complete":
434+
return cls(DownloadCompleteParams.from_json(json))
435+
else:
436+
raise ValueError("status must be either 'canceled' or 'complete'")
437+
438+
378439
class ContextCreated:
379440
"""Event class for browsingContext.contextCreated event."""
380441

@@ -523,6 +584,16 @@ def from_json(cls, json: dict):
523584
return HistoryUpdatedParams.from_json(json)
524585

525586

587+
class DownloadEnd:
588+
"""Event class for browsingContext.downloadEnd event."""
589+
590+
event_class = "browsingContext.downloadEnd"
591+
592+
@classmethod
593+
def from_json(cls, json: dict):
594+
return DownloadEndParams.from_json(json)
595+
596+
526597
@dataclass
527598
class EventConfig:
528599
event_key: str
@@ -638,6 +709,7 @@ class BrowsingContext:
638709
"context_created": EventConfig("context_created", "browsingContext.contextCreated", ContextCreated),
639710
"context_destroyed": EventConfig("context_destroyed", "browsingContext.contextDestroyed", ContextDestroyed),
640711
"dom_content_loaded": EventConfig("dom_content_loaded", "browsingContext.domContentLoaded", DomContentLoaded),
712+
"download_end": EventConfig("download_end", "browsingContext.downloadEnd", DownloadEnd),
641713
"download_will_begin": EventConfig(
642714
"download_will_begin", "browsingContext.downloadWillBegin", DownloadWillBegin
643715
),

0 commit comments

Comments
 (0)