Skip to content

Commit b539039

Browse files
committed
Fix --xvfb compatibility with --reuse-session / --rs
1 parent 08f8f3a commit b539039

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,13 @@ def __install_pyautogui_if_missing():
888888
_xvfb_display.start()
889889
sb_config._virtual_display = _xvfb_display
890890
sb_config.headless_active = True
891+
if (
892+
hasattr(sb_config, "reuse_session")
893+
and sb_config.reuse_session
894+
and hasattr(sb_config, "_vd_list")
895+
and isinstance(sb_config._vd_list, list)
896+
):
897+
sb_config._vd_list.append(_xvfb_display)
891898

892899

893900
def install_pyautogui_if_missing(driver):

seleniumbase/fixtures/base_case.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14010,6 +14010,9 @@ def __activate_standard_virtual_display(self):
1401014010
if not self.undetectable:
1401114011
sb_config._virtual_display = self._xvfb_display
1401214012
sb_config.headless_active = True
14013+
if self._reuse_session and hasattr(sb_config, "_vd_list"):
14014+
if isinstance(sb_config._vd_list, list):
14015+
sb_config._vd_list.append(self._xvfb_display)
1401314016

1401414017
def __activate_virtual_display(self):
1401514018
if self.undetectable and not (self.headless or self.headless2):
@@ -14034,6 +14037,9 @@ def __activate_virtual_display(self):
1403414037
self.__activate_standard_virtual_display()
1403514038
else:
1403614039
self.headless_active = True
14040+
if self._reuse_session and hasattr(sb_config, "_vd_list"):
14041+
if isinstance(sb_config._vd_list, list):
14042+
sb_config._vd_list.append(self._xvfb_display)
1403714043
except Exception as e:
1403814044
if hasattr(e, "msg"):
1403914045
print("\n" + str(e.msg))
@@ -14088,7 +14094,7 @@ def __activate_virtual_display_as_needed(self):
1408814094
"""This is only needed on Linux.
1408914095
The "--xvfb" arg is still useful, as it prevents headless mode,
1409014096
which is the default mode on Linux unless using another arg."""
14091-
if "linux" in sys.platform and (not self.headed or self.xvfb):
14097+
if is_linux and (not self.headed or self.xvfb):
1409214098
pip_find_lock = fasteners.InterProcessLock(
1409314099
constants.PipInstall.FINDLOCK
1409414100
)
@@ -16605,7 +16611,11 @@ def tearDown(self):
1660516611
# (Pynose / Behave / Pure Python) Close all open browser windows
1660616612
self.__quit_all_drivers()
1660716613
# Resume tearDown() for all test runners, (Pytest / Pynose / Behave)
16608-
if hasattr(self, "_xvfb_display") and self._xvfb_display:
16614+
if (
16615+
hasattr(self, "_xvfb_display")
16616+
and self._xvfb_display
16617+
and not self._reuse_session
16618+
):
1660916619
# Stop the Xvfb virtual display launched from BaseCase
1661016620
try:
1661116621
if hasattr(self._xvfb_display, "stop"):
@@ -16620,6 +16630,13 @@ def tearDown(self):
1662016630
hasattr(sb_config, "_virtual_display")
1662116631
and sb_config._virtual_display
1662216632
and hasattr(sb_config._virtual_display, "stop")
16633+
and (
16634+
not hasattr(sb_config, "reuse_session")
16635+
or (
16636+
hasattr(sb_config, "reuse_session")
16637+
and not sb_config.reuse_session
16638+
)
16639+
)
1662316640
):
1662416641
# CDP Mode may launch a 2nd Xvfb virtual display
1662516642
try:

seleniumbase/plugins/pytest_plugin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ def pytest_addoption(parser):
13711371

13721372
arg_join = " ".join(sys_argv)
13731373
sb_config._browser_shortcut = None
1374+
sb_config._vd_list = []
13741375

13751376
# SeleniumBase does not support pytest-timeout due to hanging browsers.
13761377
for arg in sys_argv:
@@ -2017,6 +2018,13 @@ def pytest_runtest_teardown(item):
20172018
hasattr(self, "_xvfb_display")
20182019
and self._xvfb_display
20192020
and hasattr(self._xvfb_display, "stop")
2021+
and (
2022+
not hasattr(sb_config, "reuse_session")
2023+
or (
2024+
hasattr(sb_config, "reuse_session")
2025+
and not sb_config.reuse_session
2026+
)
2027+
)
20202028
):
20212029
self.headless_active = False
20222030
sb_config.headless_active = False
@@ -2026,6 +2034,13 @@ def pytest_runtest_teardown(item):
20262034
hasattr(sb_config, "_virtual_display")
20272035
and sb_config._virtual_display
20282036
and hasattr(sb_config._virtual_display, "stop")
2037+
and (
2038+
not hasattr(sb_config, "reuse_session")
2039+
or (
2040+
hasattr(sb_config, "reuse_session")
2041+
and not sb_config.reuse_session
2042+
)
2043+
)
20292044
):
20302045
sb_config._virtual_display.stop()
20312046
sb_config._virtual_display = None
@@ -2139,6 +2154,21 @@ def _perform_pytest_unconfigure_(config):
21392154
except Exception:
21402155
pass
21412156
sb_config.shared_driver = None
2157+
with suppress(Exception):
2158+
if (
2159+
hasattr(sb_config, "_virtual_display")
2160+
and sb_config._virtual_display
2161+
and hasattr(sb_config._virtual_display, "stop")
2162+
):
2163+
sb_config._virtual_display.stop()
2164+
sb_config._virtual_display = None
2165+
sb_config.headless_active = False
2166+
if hasattr(sb_config, "_vd_list") and sb_config._vd_list:
2167+
if isinstance(sb_config._vd_list, list):
2168+
for display in sb_config._vd_list:
2169+
if display:
2170+
with suppress(Exception):
2171+
display.stop()
21422172
if hasattr(sb_config, "log_path") and sb_config.item_count > 0:
21432173
log_helper.archive_logs_if_set(
21442174
constants.Logs.LATEST + "/", sb_config.archive_logs

0 commit comments

Comments
 (0)