Path: blob/trunk/py/selenium/webdriver/common/bidi/session.py
4014 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.161718from selenium.webdriver.common.bidi.common import command_builder192021class UserPromptHandlerType:22"""Represents the behavior of the user prompt handler."""2324ACCEPT = "accept"25DISMISS = "dismiss"26IGNORE = "ignore"2728VALID_TYPES = {ACCEPT, DISMISS, IGNORE}293031class UserPromptHandler:32"""Represents the configuration of the user prompt handler."""3334def __init__(35self,36alert: str | None = None,37before_unload: str | None = None,38confirm: str | None = None,39default: str | None = None,40file: str | None = None,41prompt: str | None = None,42):43"""Initialize UserPromptHandler.4445Args:46alert: Handler type for alert prompts.47before_unload: Handler type for beforeUnload prompts.48confirm: Handler type for confirm prompts.49default: Default handler type for all prompts.50file: Handler type for file picker prompts.51prompt: Handler type for prompt dialogs.5253Raises:54ValueError: If any handler type is not valid.55"""56for field_name, value in [57("alert", alert),58("before_unload", before_unload),59("confirm", confirm),60("default", default),61("file", file),62("prompt", prompt),63]:64if value is not None and value not in UserPromptHandlerType.VALID_TYPES:65raise ValueError(66f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}"67)6869self.alert = alert70self.before_unload = before_unload71self.confirm = confirm72self.default = default73self.file = file74self.prompt = prompt7576def to_dict(self) -> dict[str, str]:77"""Convert the UserPromptHandler to a dictionary for BiDi protocol.7879Returns:80Dictionary representation suitable for BiDi protocol.81"""82field_mapping = {83"alert": "alert",84"before_unload": "beforeUnload",85"confirm": "confirm",86"default": "default",87"file": "file",88"prompt": "prompt",89}9091result = {}92for attr_name, dict_key in field_mapping.items():93value = getattr(self, attr_name)94if value is not None:95result[dict_key] = value96return result979899class Session:100def __init__(self, conn):101self.conn = conn102103def subscribe(self, *events, browsing_contexts=None):104params = {105"events": events,106}107if browsing_contexts is None:108browsing_contexts = []109if browsing_contexts:110params["browsingContexts"] = browsing_contexts111return command_builder("session.subscribe", params)112113def unsubscribe(self, *events, browsing_contexts=None):114params = {115"events": events,116}117if browsing_contexts is None:118browsing_contexts = []119if browsing_contexts:120params["browsingContexts"] = browsing_contexts121return command_builder("session.unsubscribe", params)122123def status(self):124"""The session.status command returns information about the remote end's readiness.125126Returns information about the remote end's readiness to create new sessions127and may include implementation-specific metadata.128129Returns:130Dictionary containing the ready state (bool), message (str) and metadata.131"""132cmd = command_builder("session.status", {})133return self.conn.execute(cmd)134135136