Skip to content

Commit d7d3e26

Browse files
committed
✨ Enhance CI compatibility for Selenium tests
- Automatically detect CI environment for local testing configuration. - Update Chrome WebDriver initialization to prioritize chromedriver from PATH, CHROMEDRIVER_PATH, or fallback to webdriver-manager. - Improve logging preferences setup for Selenium 4.x. - Maintain backward compatibility with hardcoded chromedriver paths for local development.
1 parent 177aeff commit d7d3e26

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

tests/conftest.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
_nb_samples = 10_000
2121
data_dir = "examples/data/"
2222
headless = True
23-
TESTING_LOCAL = False # SET THIS TO TRUE IF YOU ARE TESTING LOCALLY
23+
# Automatically detect if running in CI (GitHub Actions, etc.)
24+
TESTING_LOCAL = (
25+
os.environ.get("CI") not in ("true", "1", "True")
26+
and os.environ.get("GITHUB_ACTIONS") != "true"
27+
)
2428

2529

2630
@pytest.fixture
@@ -46,38 +50,67 @@ def pickle_figure():
4650

4751
@pytest.fixture
4852
def driver():
53+
import os
54+
import shutil
4955
import time
5056

5157
from selenium.webdriver.chrome.options import Options
52-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
58+
from selenium.webdriver.chrome.service import Service
5359
from seleniumwire import webdriver
60+
from webdriver_manager.chrome import ChromeDriverManager
5461

5562
time.sleep(3)
5663

5764
options = Options()
58-
d = DesiredCapabilities.CHROME
59-
d["goog:loggingPrefs"] = {"browser": "ALL"}
65+
# Set logging preferences via options (replaces DesiredCapabilities in Selenium 4.x)
66+
options.set_capability("goog:loggingPrefs", {"browser": "ALL"})
67+
6068
if not TESTING_LOCAL:
69+
# CI environment (GitHub Actions)
6170
if headless:
6271
options.add_argument("--headless")
6372
options.add_argument("--no-sandbox")
6473
options.add_argument("--disable-dev-shm-usage")
6574
options.add_argument("--disable-gpu")
66-
# options.add_argument("--no=sandbox")
6775

68-
driver = webdriver.Chrome(
69-
options=options,
70-
desired_capabilities=d,
71-
)
76+
# In CI, try ChromeDriver from PATH first (set by setup-chromedriver action)
77+
# The setup-chromedriver action adds chromedriver to PATH
78+
# Strategy: Try PATH first, then CHROMEDRIVER_PATH env var, then webdriver-manager
79+
# Check if chromedriver is available in PATH
80+
chromedriver_in_path = shutil.which("chromedriver")
81+
if chromedriver_in_path:
82+
# Use chromedriver from PATH (installed by setup-chromedriver action)
83+
service = Service(chromedriver_in_path)
84+
else:
85+
# Try CHROMEDRIVER_PATH environment variable
86+
chromedriver_path = os.environ.get("CHROMEDRIVER_PATH")
87+
if chromedriver_path and os.path.exists(chromedriver_path):
88+
service = Service(chromedriver_path)
89+
else:
90+
# Fall back to webdriver-manager to auto-download correct version
91+
service = Service(ChromeDriverManager().install())
7292
else:
93+
# Local development environment
7394
options.add_argument("--remote-debugging-port=9222")
74-
driver = webdriver.Chrome(
75-
options=options,
76-
# executable_path="/home/jeroen/chromedriver",
77-
# executable_path="/home/jonas/Documents/chromedriver-linux64/chromedriver",
78-
desired_capabilities=d,
79-
)
80-
# driver = webdriver.Firefox(executable_path='/home/jonas/git/gIDLaB/plotly-dynamic-resampling/geckodriver')
95+
96+
# Try hardcoded path first (for backward compatibility)
97+
hardcoded_path = "/home/jonas/Documents/chromedriver-linux64/chromedriver"
98+
if os.path.exists(hardcoded_path):
99+
try:
100+
# Try the hardcoded path first
101+
service = Service(executable_path=hardcoded_path)
102+
# Test if it works by attempting to create driver (will raise if version mismatch)
103+
test_driver = webdriver.Chrome(options=options, service=service)
104+
test_driver.quit()
105+
# If we get here, the hardcoded path works
106+
except Exception:
107+
# Version mismatch or other error, use webdriver-manager
108+
service = Service(ChromeDriverManager().install())
109+
else:
110+
# Use webdriver-manager to auto-download correct version
111+
service = Service(ChromeDriverManager().install())
112+
113+
driver = webdriver.Chrome(options=options, service=service)
81114
return driver
82115

83116

0 commit comments

Comments
 (0)