Skip to content

Commit 6c3eda3

Browse files
fixed mypy errors
1 parent 472f5be commit 6c3eda3

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

py/selenium/webdriver/common/bidi/browser.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
1819
import warnings
19-
from typing import Optional
20+
from typing import Any, Optional
2021

2122
from selenium.webdriver.common.bidi.common import command_builder
23+
from selenium.webdriver.common.bidi.session import UserPromptHandler
2224
from selenium.webdriver.common.proxy import Proxy
2325

2426

@@ -243,7 +245,7 @@ def y(self, value) -> None:
243245
self._y = value
244246

245247
@property
246-
def active(self):
248+
def active(self) -> bool:
247249
"""Gets the Window Status.
248250
249251
Returns:
@@ -269,6 +271,7 @@ def is_active(self) -> bool:
269271
-------
270272
bool: True if the client window is active, False otherwise.
271273
"""
274+
warnings.warn("is_active method is deprecated, use `active` property instead", DeprecationWarning, stacklevel=2)
272275
return self.active
273276

274277
@classmethod
@@ -284,13 +287,13 @@ def from_dict(cls, data: dict) -> "ClientWindowInfo":
284287
ClientWindowInfo: A new instance of ClientWindowInfo.
285288
"""
286289
return cls(
287-
client_window=data.get("clientWindow"),
288-
state=data.get("state"),
289-
width=data.get("width"),
290-
height=data.get("height"),
291-
x=data.get("x"),
292-
y=data.get("y"),
293-
active=data.get("active"),
290+
client_window=data["clientWindow"],
291+
state=data["state"],
292+
width=data["width"],
293+
height=data["height"],
294+
x=data["x"],
295+
y=data["y"],
296+
active=data["active"],
294297
)
295298

296299

@@ -300,26 +303,35 @@ class Browser:
300303
def __init__(self, conn):
301304
self.conn = conn
302305

303-
def create_user_context(self, accept_insecure_certs: Optional[bool] = None, proxy: Optional[Proxy] = None) -> str:
306+
def create_user_context(
307+
self,
308+
accept_insecure_certs: Optional[bool] = None,
309+
proxy: Optional[Proxy] = None,
310+
unhandled_prompt_behavior: Optional[UserPromptHandler] = None,
311+
) -> str:
304312
"""Creates a new user context.
305313
306314
Parameters:
307315
-----------
308316
accept_insecure_certs: Optional flag to accept insecure TLS certificates
309317
proxy: Optional proxy configuration for the user context
318+
unhandled_prompt_behavior: Optional configuration for handling user prompts
310319
311320
Returns:
312321
-------
313322
str: The ID of the created user context.
314323
"""
315-
params = {}
324+
params: dict[str, Any] = {}
316325

317326
if accept_insecure_certs is not None:
318327
params["acceptInsecureCerts"] = accept_insecure_certs
319328

320329
if proxy is not None:
321330
params["proxy"] = proxy.to_bidi_dict()
322331

332+
if unhandled_prompt_behavior is not None:
333+
params["unhandledPromptBehavior"] = unhandled_prompt_behavior.to_dict()
334+
323335
result = self.conn.execute(command_builder("browser.createUserContext", params))
324336
return result["userContext"]
325337

py/test/selenium/webdriver/common/bidi_browser_tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pytest
2323

2424
from selenium.webdriver.common.bidi.browser import ClientWindowInfo, ClientWindowState
25+
from selenium.webdriver.common.bidi.session import UserPromptHandler, UserPromptHandlerType
2526
from selenium.webdriver.common.by import By
2627
from selenium.webdriver.common.proxy import Proxy, ProxyType
2728
from selenium.webdriver.common.utils import free_port
@@ -235,3 +236,29 @@ def test_create_user_context_with_proxy_and_accept_insecure_certs(driver):
235236
driver.browser.remove_user_context(user_context)
236237
fake_proxy_server.shutdown()
237238
fake_proxy_server.server_close()
239+
240+
241+
def test_create_user_context_with_unhandled_prompt_behavior(driver, pages):
242+
"""Test creating a user context with unhandled prompt behavior configuration."""
243+
prompt_handler = UserPromptHandler(
244+
alert=UserPromptHandlerType.DISMISS, default=UserPromptHandlerType.DISMISS, prompt=UserPromptHandlerType.DISMISS
245+
)
246+
247+
user_context = driver.browser.create_user_context(unhandled_prompt_behavior=prompt_handler)
248+
assert user_context is not None
249+
250+
# create a new browsing context with the user context
251+
bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context)
252+
assert bc is not None
253+
254+
driver.switch_to.window(bc)
255+
pages.load("alerts.html")
256+
257+
# TODO: trigger an alert and test that it is dismissed automatically, currently not working,
258+
# conftest.py unhandled_prompt_behavior set to IGNORE, see if it is related
259+
# driver.find_element(By.ID, "alert").click()
260+
# # accessing title should be possible since alert is auto handled
261+
# assert driver.title == "Testing Alerts"
262+
263+
# Clean up
264+
driver.browser.remove_user_context(user_context)

0 commit comments

Comments
 (0)