|
| 1 | +import os |
1 | 2 | import time |
2 | 3 |
|
3 | 4 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
4 | 5 | from django.test import tag |
5 | 6 | from django.urls.base import reverse |
| 7 | +from selenium import webdriver |
6 | 8 | from selenium.common.exceptions import TimeoutException |
7 | 9 | from selenium.webdriver.common.action_chains import ActionChains |
8 | 10 | from selenium.webdriver.common.by import By |
9 | 11 | from selenium.webdriver.common.keys import Keys |
| 12 | +from selenium.webdriver.common.utils import free_port |
10 | 13 | from selenium.webdriver.support import expected_conditions as EC |
11 | 14 | from selenium.webdriver.support.ui import Select, WebDriverWait |
12 | 15 | from swapper import load_model |
@@ -498,6 +501,37 @@ class TestDeviceAdminUnsavedChanges( |
498 | 501 | ): |
499 | 502 | browser = "chrome" |
500 | 503 |
|
| 504 | + @classmethod |
| 505 | + def get_chrome_webdriver(cls): |
| 506 | + """ |
| 507 | + Override the parent class method to enable BiDi mode and set |
| 508 | + unhandledPromptBehavior to "ignore". This is required to test |
| 509 | + beforeunload alerts, as Chromium v126+ auto-accepts them per |
| 510 | + WebDriver standard. |
| 511 | +
|
| 512 | + Ref: https://github.com/openwisp/openwisp-controller/issues/902 |
| 513 | + """ |
| 514 | + options = webdriver.ChromeOptions() |
| 515 | + options.page_load_strategy = "eager" |
| 516 | + if os.environ.get("SELENIUM_HEADLESS", False): |
| 517 | + options.add_argument("--headless") |
| 518 | + CHROME_BIN = os.environ.get("CHROME_BIN", None) |
| 519 | + if CHROME_BIN: |
| 520 | + options.binary_location = CHROME_BIN |
| 521 | + options.add_argument("--window-size=1366,768") |
| 522 | + options.add_argument("--ignore-certificate-errors") |
| 523 | + options.add_argument("--no-sandbox") |
| 524 | + options.add_argument("--disable-gpu") |
| 525 | + options.add_argument("--disable-dev-shm-usage") |
| 526 | + options.add_argument("--disable-features=VizDisplayCompositor") |
| 527 | + options.add_argument(f"--remote-debugging-port={free_port()}") |
| 528 | + options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) |
| 529 | + # Enable BiDi mode and set unhandledPromptBehavior to "ignore" |
| 530 | + # to allow testing beforeunload alerts (Chromium v126+). |
| 531 | + options.enable_bidi = True |
| 532 | + options.set_capability("unhandledPromptBehavior", "ignore") |
| 533 | + return webdriver.Chrome(options=options) |
| 534 | + |
501 | 535 | def _is_unsaved_changes_alert_present(self): |
502 | 536 | for entry in self.get_browser_logs(): |
503 | 537 | if ( |
|
0 commit comments