Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/bidi/session.py
1864 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
from typing import Optional
19
20
from selenium.webdriver.common.bidi.common import command_builder
21
22
23
class UserPromptHandlerType:
24
"""Represents the behavior of the user prompt handler."""
25
26
ACCEPT = "accept"
27
DISMISS = "dismiss"
28
IGNORE = "ignore"
29
30
VALID_TYPES = {ACCEPT, DISMISS, IGNORE}
31
32
33
class UserPromptHandler:
34
"""Represents the configuration of the user prompt handler."""
35
36
def __init__(
37
self,
38
alert: Optional[str] = None,
39
before_unload: Optional[str] = None,
40
confirm: Optional[str] = None,
41
default: Optional[str] = None,
42
file: Optional[str] = None,
43
prompt: Optional[str] = None,
44
):
45
"""Initialize UserPromptHandler.
46
47
Parameters:
48
-----------
49
alert: Handler type for alert prompts
50
before_unload: Handler type for beforeUnload prompts
51
confirm: Handler type for confirm prompts
52
default: Default handler type for all prompts
53
file: Handler type for file picker prompts
54
prompt: Handler type for prompt dialogs
55
56
Raises:
57
------
58
ValueError: If any handler type is not valid
59
"""
60
for field_name, value in [
61
("alert", alert),
62
("before_unload", before_unload),
63
("confirm", confirm),
64
("default", default),
65
("file", file),
66
("prompt", prompt),
67
]:
68
if value is not None and value not in UserPromptHandlerType.VALID_TYPES:
69
raise ValueError(
70
f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}"
71
)
72
73
self.alert = alert
74
self.before_unload = before_unload
75
self.confirm = confirm
76
self.default = default
77
self.file = file
78
self.prompt = prompt
79
80
def to_dict(self) -> dict[str, str]:
81
"""Convert the UserPromptHandler to a dictionary for BiDi protocol.
82
83
Returns:
84
-------
85
Dict[str, str]: Dictionary representation suitable for BiDi protocol
86
"""
87
field_mapping = {
88
"alert": "alert",
89
"before_unload": "beforeUnload",
90
"confirm": "confirm",
91
"default": "default",
92
"file": "file",
93
"prompt": "prompt",
94
}
95
96
result = {}
97
for attr_name, dict_key in field_mapping.items():
98
value = getattr(self, attr_name)
99
if value is not None:
100
result[dict_key] = value
101
return result
102
103
104
class Session:
105
def __init__(self, conn):
106
self.conn = conn
107
108
def subscribe(self, *events, browsing_contexts=None):
109
params = {
110
"events": events,
111
}
112
if browsing_contexts is None:
113
browsing_contexts = []
114
if browsing_contexts:
115
params["browsingContexts"] = browsing_contexts
116
return command_builder("session.subscribe", params)
117
118
def unsubscribe(self, *events, browsing_contexts=None):
119
params = {
120
"events": events,
121
}
122
if browsing_contexts is None:
123
browsing_contexts = []
124
if browsing_contexts:
125
params["browsingContexts"] = browsing_contexts
126
return command_builder("session.unsubscribe", params)
127
128
def status(self):
129
"""
130
The session.status command returns information about the remote end's readiness
131
to create new sessions and may include implementation-specific metadata.
132
133
Returns
134
-------
135
dict
136
Dictionary containing the ready state (bool), message (str) and metadata
137
"""
138
cmd = command_builder("session.status", {})
139
return self.conn.execute(cmd)
140
141