Path: blob/trunk/py/selenium/webdriver/common/bidi/session.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from typing import Optional1819from selenium.webdriver.common.bidi.common import command_builder202122class UserPromptHandlerType:23"""Represents the behavior of the user prompt handler."""2425ACCEPT = "accept"26DISMISS = "dismiss"27IGNORE = "ignore"2829VALID_TYPES = {ACCEPT, DISMISS, IGNORE}303132class UserPromptHandler:33"""Represents the configuration of the user prompt handler."""3435def __init__(36self,37alert: Optional[str] = None,38before_unload: Optional[str] = None,39confirm: Optional[str] = None,40default: Optional[str] = None,41file: Optional[str] = None,42prompt: Optional[str] = None,43):44"""Initialize UserPromptHandler.4546Parameters:47-----------48alert: Handler type for alert prompts49before_unload: Handler type for beforeUnload prompts50confirm: Handler type for confirm prompts51default: Default handler type for all prompts52file: Handler type for file picker prompts53prompt: Handler type for prompt dialogs5455Raises:56------57ValueError: If any handler type is not valid58"""59for field_name, value in [60("alert", alert),61("before_unload", before_unload),62("confirm", confirm),63("default", default),64("file", file),65("prompt", prompt),66]:67if value is not None and value not in UserPromptHandlerType.VALID_TYPES:68raise ValueError(69f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}"70)7172self.alert = alert73self.before_unload = before_unload74self.confirm = confirm75self.default = default76self.file = file77self.prompt = prompt7879def to_dict(self) -> dict[str, str]:80"""Convert the UserPromptHandler to a dictionary for BiDi protocol.8182Returns:83-------84Dict[str, str]: Dictionary representation suitable for BiDi protocol85"""86field_mapping = {87"alert": "alert",88"before_unload": "beforeUnload",89"confirm": "confirm",90"default": "default",91"file": "file",92"prompt": "prompt",93}9495result = {}96for attr_name, dict_key in field_mapping.items():97value = getattr(self, attr_name)98if value is not None:99result[dict_key] = value100return result101102103class Session:104def __init__(self, conn):105self.conn = conn106107def subscribe(self, *events, browsing_contexts=None):108params = {109"events": events,110}111if browsing_contexts is None:112browsing_contexts = []113if browsing_contexts:114params["browsingContexts"] = browsing_contexts115return command_builder("session.subscribe", params)116117def unsubscribe(self, *events, browsing_contexts=None):118params = {119"events": events,120}121if browsing_contexts is None:122browsing_contexts = []123if browsing_contexts:124params["browsingContexts"] = browsing_contexts125return command_builder("session.unsubscribe", params)126127def status(self):128"""129The session.status command returns information about the remote end's readiness130to create new sessions and may include implementation-specific metadata.131132Returns133-------134dict135Dictionary containing the ready state (bool), message (str) and metadata136"""137cmd = command_builder("session.status", {})138return self.conn.execute(cmd)139140141