|
16 | 16 | # under the License. |
17 | 17 |
|
18 | 18 | import http.server |
| 19 | +import os |
19 | 20 | import socketserver |
20 | 21 | import threading |
21 | 22 |
|
22 | 23 | import pytest |
23 | 24 |
|
| 25 | +from selenium.common.exceptions import TimeoutException |
24 | 26 | from selenium.webdriver.common.bidi.browser import ClientWindowInfo, ClientWindowState |
| 27 | +from selenium.webdriver.common.bidi.browsing_context import ReadinessState |
25 | 28 | from selenium.webdriver.common.bidi.session import UserPromptHandler, UserPromptHandlerType |
26 | 29 | from selenium.webdriver.common.by import By |
27 | 30 | from selenium.webdriver.common.proxy import Proxy, ProxyType |
28 | 31 | from selenium.webdriver.common.utils import free_port |
29 | 32 | from selenium.webdriver.common.window import WindowTypes |
| 33 | +from selenium.webdriver.support.ui import WebDriverWait |
30 | 34 |
|
31 | 35 |
|
32 | 36 | class FakeProxyHandler(http.server.SimpleHTTPRequestHandler): |
@@ -262,3 +266,96 @@ def test_create_user_context_with_unhandled_prompt_behavior(driver, pages): |
262 | 266 |
|
263 | 267 | # Clean up |
264 | 268 | driver.browser.remove_user_context(user_context) |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.xfail_firefox |
| 272 | +def test_set_download_behavior_allowed(driver, pages, tmp_path): |
| 273 | + print(f"Driver info: {driver.capabilities}") |
| 274 | + try: |
| 275 | + driver.browser.set_download_behavior(allowed=True, destination_folder=tmp_path) |
| 276 | + |
| 277 | + context_id = driver.current_window_handle |
| 278 | + url = pages.url("downloads/download.html") |
| 279 | + driver.browsing_context.navigate(context=context_id, url=url, wait=ReadinessState.COMPLETE) |
| 280 | + |
| 281 | + driver.find_element(By.ID, "file-1").click() |
| 282 | + |
| 283 | + WebDriverWait(driver, 5).until(lambda d: "file_1.txt" in os.listdir(tmp_path)) |
| 284 | + |
| 285 | + files = os.listdir(tmp_path) |
| 286 | + assert "file_1.txt" in files, f"Expected file_1.txt in {tmp_path}, but found: {files}" |
| 287 | + finally: |
| 288 | + driver.browser.set_download_behavior(allowed=None) |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.xfail_firefox |
| 292 | +def test_set_download_behavior_denied(driver, pages, tmp_path): |
| 293 | + try: |
| 294 | + driver.browser.set_download_behavior(allowed=False) |
| 295 | + |
| 296 | + context_id = driver.current_window_handle |
| 297 | + url = pages.url("downloads/download.html") |
| 298 | + driver.browsing_context.navigate(context=context_id, url=url, wait=ReadinessState.COMPLETE) |
| 299 | + |
| 300 | + driver.find_element(By.ID, "file-1").click() |
| 301 | + |
| 302 | + try: |
| 303 | + WebDriverWait(driver, 3, poll_frequency=0.2).until(lambda _: len(os.listdir(tmp_path)) > 0) |
| 304 | + files = os.listdir(tmp_path) |
| 305 | + pytest.fail(f"A file was downloaded unexpectedly: {files}") |
| 306 | + except TimeoutException: |
| 307 | + pass # Expected, no file downloaded |
| 308 | + finally: |
| 309 | + driver.browser.set_download_behavior(allowed=None) |
| 310 | + |
| 311 | + |
| 312 | +@pytest.mark.xfail_firefox |
| 313 | +def test_set_download_behavior_user_context(driver, pages, tmp_path): |
| 314 | + user_context = driver.browser.create_user_context() |
| 315 | + |
| 316 | + try: |
| 317 | + bc = driver.browsing_context.create(type=WindowTypes.WINDOW, user_context=user_context) |
| 318 | + driver.switch_to.window(bc) |
| 319 | + |
| 320 | + try: |
| 321 | + driver.browser.set_download_behavior( |
| 322 | + allowed=True, destination_folder=tmp_path, user_contexts=[user_context] |
| 323 | + ) |
| 324 | + |
| 325 | + url = pages.url("downloads/download.html") |
| 326 | + driver.browsing_context.navigate(context=bc, url=url, wait=ReadinessState.COMPLETE) |
| 327 | + |
| 328 | + driver.find_element(By.ID, "file-1").click() |
| 329 | + |
| 330 | + WebDriverWait(driver, 5).until(lambda d: "file_1.txt" in os.listdir(tmp_path)) |
| 331 | + |
| 332 | + files = os.listdir(tmp_path) |
| 333 | + assert "file_1.txt" in files, f"Expected file_1.txt in {tmp_path}, but found: {files}" |
| 334 | + |
| 335 | + initial_file_count = len(files) |
| 336 | + |
| 337 | + driver.browser.set_download_behavior(allowed=False, user_contexts=[user_context]) |
| 338 | + |
| 339 | + driver.find_element(By.ID, "file-2").click() |
| 340 | + |
| 341 | + try: |
| 342 | + WebDriverWait(driver, 3, poll_frequency=0.2).until( |
| 343 | + lambda _: len(os.listdir(tmp_path)) > initial_file_count |
| 344 | + ) |
| 345 | + files_after = os.listdir(tmp_path) |
| 346 | + pytest.fail(f"A file was downloaded unexpectedly: {files_after}") |
| 347 | + except TimeoutException: |
| 348 | + pass # Expected, no file downloaded |
| 349 | + finally: |
| 350 | + driver.browser.set_download_behavior(allowed=None, user_contexts=[user_context]) |
| 351 | + finally: |
| 352 | + driver.browser.remove_user_context(user_context) |
| 353 | + |
| 354 | + |
| 355 | +@pytest.mark.xfail_firefox |
| 356 | +def test_set_download_behavior_validation(driver): |
| 357 | + with pytest.raises(ValueError, match="destination_folder is required when allowed=True"): |
| 358 | + driver.browser.set_download_behavior(allowed=True) |
| 359 | + |
| 360 | + with pytest.raises(ValueError, match="destination_folder should not be provided when allowed=False"): |
| 361 | + driver.browser.set_download_behavior(allowed=False, destination_folder="/tmp") |
0 commit comments