Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/bidi/session.py
4014 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
19
from selenium.webdriver.common.bidi.common import command_builder
20
21
22
class UserPromptHandlerType:
23
"""Represents the behavior of the user prompt handler."""
24
25
ACCEPT = "accept"
26
DISMISS = "dismiss"
27
IGNORE = "ignore"
28
29
VALID_TYPES = {ACCEPT, DISMISS, IGNORE}
30
31
32
class UserPromptHandler:
33
"""Represents the configuration of the user prompt handler."""
34
35
def __init__(
36
self,
37
alert: str | None = None,
38
before_unload: str | None = None,
39
confirm: str | None = None,
40
default: str | None = None,
41
file: str | None = None,
42
prompt: str | None = None,
43
):
44
"""Initialize UserPromptHandler.
45
46
Args:
47
alert: Handler type for alert prompts.
48
before_unload: Handler type for beforeUnload prompts.
49
confirm: Handler type for confirm prompts.
50
default: Default handler type for all prompts.
51
file: Handler type for file picker prompts.
52
prompt: Handler type for prompt dialogs.
53
54
Raises:
55
ValueError: If any handler type is not valid.
56
"""
57
for field_name, value in [
58
("alert", alert),
59
("before_unload", before_unload),
60
("confirm", confirm),
61
("default", default),
62
("file", file),
63
("prompt", prompt),
64
]:
65
if value is not None and value not in UserPromptHandlerType.VALID_TYPES:
66
raise ValueError(
67
f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}"
68
)
69
70
self.alert = alert
71
self.before_unload = before_unload
72
self.confirm = confirm
73
self.default = default
74
self.file = file
75
self.prompt = prompt
76
77
def to_dict(self) -> dict[str, str]:
78
"""Convert the UserPromptHandler to a dictionary for BiDi protocol.
79
80
Returns:
81
Dictionary representation suitable for BiDi protocol.
82
"""
83
field_mapping = {
84
"alert": "alert",
85
"before_unload": "beforeUnload",
86
"confirm": "confirm",
87
"default": "default",
88
"file": "file",
89
"prompt": "prompt",
90
}
91
92
result = {}
93
for attr_name, dict_key in field_mapping.items():
94
value = getattr(self, attr_name)
95
if value is not None:
96
result[dict_key] = value
97
return result
98
99
100
class Session:
101
def __init__(self, conn):
102
self.conn = conn
103
104
def subscribe(self, *events, browsing_contexts=None):
105
params = {
106
"events": events,
107
}
108
if browsing_contexts is None:
109
browsing_contexts = []
110
if browsing_contexts:
111
params["browsingContexts"] = browsing_contexts
112
return command_builder("session.subscribe", params)
113
114
def unsubscribe(self, *events, browsing_contexts=None):
115
params = {
116
"events": events,
117
}
118
if browsing_contexts is None:
119
browsing_contexts = []
120
if browsing_contexts:
121
params["browsingContexts"] = browsing_contexts
122
return command_builder("session.unsubscribe", params)
123
124
def status(self):
125
"""The session.status command returns information about the remote end's readiness.
126
127
Returns information about the remote end's readiness to create new sessions
128
and may include implementation-specific metadata.
129
130
Returns:
131
Dictionary containing the ready state (bool), message (str) and metadata.
132
"""
133
cmd = command_builder("session.status", {})
134
return self.conn.execute(cmd)
135
136