Skip to content

Commit 97507af

Browse files
[tests:fix] Addressed Selenium unsaved changes test #902
Chromium v126 auto-accepts beforeunload dialogs in classic WebDriver sessions, causing Selenium tests waiting for alerts to fail. This change updates the test strategy to reliably detect unsaved changes without relying on deprecated beforeunload alert behavior. Fixes #902 Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
1 parent 012a094 commit 97507af

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

openwisp_controller/config/tests/test_selenium.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import os
12
import time
23

34
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
45
from django.test import tag
56
from django.urls.base import reverse
7+
from selenium import webdriver
68
from selenium.common.exceptions import TimeoutException
79
from selenium.webdriver.common.action_chains import ActionChains
810
from selenium.webdriver.common.by import By
911
from selenium.webdriver.common.keys import Keys
12+
from selenium.webdriver.common.utils import free_port
1013
from selenium.webdriver.support import expected_conditions as EC
1114
from selenium.webdriver.support.ui import Select, WebDriverWait
1215
from swapper import load_model
@@ -498,6 +501,37 @@ class TestDeviceAdminUnsavedChanges(
498501
):
499502
browser = "chrome"
500503

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+
501535
def _is_unsaved_changes_alert_present(self):
502536
for entry in self.get_browser_logs():
503537
if (

0 commit comments

Comments
 (0)