[py][bidi]: add `downloadEnd` event for browsing context by navin772 · Pull Request #16209 · SeleniumHQ/selenium
PR Code Suggestions ✨
Explore these optional code suggestions:
| Category | Suggestion | Impact |
| High-level |
Validate spec fields and typesThe new DownloadEnd parsing assumes only "status" plus basic NavigationInfo Examples:py/selenium/webdriver/common/bidi/browsing_context.py [354-380]class DownloadCanceledParams(NavigationInfo): def __init__( self, context: str, navigation: Optional[str], timestamp: int, url: str, status: str = "canceled", ): super().__init__(context, navigation, timestamp, url) ... (clipped 17 lines) py/selenium/webdriver/common/bidi/browsing_context.py [428-436]@classmethod def from_json(cls, json: dict) -> "DownloadEndParams": status = json.get("status") if status == "canceled": return cls(DownloadCanceledParams.from_json(json)) elif status == "complete": return cls(DownloadCompleteParams.from_json(json)) else: raise ValueError("status must be either 'canceled' or 'complete'") Solution Walkthrough:Before:class DownloadCanceledParams(NavigationInfo): # ... # Missing `reason` field. class DownloadEndParams: @classmethod def from_json(cls, json: dict): status = json.get("status") if status == "canceled": return cls(DownloadCanceledParams.from_json(json)) elif status == "complete": return cls(DownloadCompleteParams.from_json(json)) else: raise ValueError("status must be 'canceled' or 'complete'") After:class DownloadCanceledParams(NavigationInfo): def __init__(self, ..., reason: str): super().__init__(...) self.reason = reason # ... from_json also parses `reason` class DownloadErrorParams(NavigationInfo): # ... New class to handle 'error' status with a 'reason' field. class DownloadEndParams: @classmethod def from_json(cls, json: dict): status = json.get("status") if status == "canceled": return cls(DownloadCanceledParams.from_json(json)) elif status == "complete": return cls(DownloadCompleteParams.from_json(json)) elif status == "error": return cls(DownloadErrorParams.from_json(json)) else: raise ValueError("Unsupported download status from spec") Suggestion importance[1-10]: 9__ Why: This suggestion correctly identifies a critical flaw where the implementation diverges from the WebDriver BiDi spec by omitting the | High |
| Possible issue |
Guard against non-dict inputsValidate that py/selenium/webdriver/common/bidi/browsing_context.py [419-436] class DownloadEndParams:
"""Parameters for the downloadEnd event."""
def __init__(
self,
download_params: Union[DownloadCanceledParams, DownloadCompleteParams],
):
self.download_params = download_params
@classmethod
def from_json(cls, json: dict) -> "DownloadEndParams":
+ if not isinstance(json, dict):
+ raise ValueError("json must be a dictionary")
status = json.get("status")
if status == "canceled":
return cls(DownloadCanceledParams.from_json(json))
elif status == "complete":
return cls(DownloadCompleteParams.from_json(json))
else:
raise ValueError("status must be either 'canceled' or 'complete'")
Suggestion importance[1-10]: 6__ Why: The suggestion correctly adds a check to ensure the | Low |
| General |
Validate status type strictlyEnsure py/selenium/webdriver/common/bidi/browsing_context.py [354-380] class DownloadCanceledParams(NavigationInfo):
def __init__(
self,
context: str,
navigation: Optional[str],
timestamp: int,
url: str,
status: str = "canceled",
):
super().__init__(context, navigation, timestamp, url)
self.status = status
@classmethod
def from_json(cls, json: dict) -> "DownloadCanceledParams":
nav_info = NavigationInfo.from_json(json)
status = json.get("status")
- if status is None or status != "canceled":
+ if not isinstance(status, str) or status != "canceled":
raise ValueError("status is required and must be 'canceled'")
return cls(
context=nav_info.context,
navigation=nav_info.navigation,
timestamp=nav_info.timestamp,
url=nav_info.url,
status=status,
)
Suggestion importance[1-10]: 6__ Why: The suggestion improves the validation logic by adding a strict type check for the | Low |
Normalize empty filepath valuesNormalize the optional py/selenium/webdriver/common/bidi/browsing_context.py [383-416] class DownloadCompleteParams(NavigationInfo):
def __init__(
self,
context: str,
navigation: Optional[str],
timestamp: int,
url: str,
status: str = "complete",
filepath: Optional[str] = None,
):
super().__init__(context, navigation, timestamp, url)
self.status = status
self.filepath = filepath
@classmethod
def from_json(cls, json: dict) -> "DownloadCompleteParams":
nav_info = NavigationInfo.from_json(json)
status = json.get("status")
if status is None or status != "complete":
raise ValueError("status is required and must be 'complete'")
filepath = json.get("filepath")
if filepath is not None and not isinstance(filepath, str):
raise ValueError("filepath must be a string if provided")
+ # Normalize empty string to None
+ if isinstance(filepath, str) and filepath.strip() == "":
+ filepath = None
return cls(
context=nav_info.context,
navigation=nav_info.navigation,
timestamp=nav_info.timestamp,
url=nav_info.url,
status=status,
filepath=filepath,
)
Suggestion importance[1-10]: 5__ Why: The suggestion to normalize an empty string | Low | |
| ||