Skip to content

Commit c031241

Browse files
committed
Fix "sb" fixture issue that occurs with "--pdb" on failures
1 parent ea61c9c commit c031241

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11173,6 +11173,8 @@ def setUp(self, masterqa_mode=False):
1117311173
self.test_id = test_id
1117411174
if hasattr(self, "_using_sb_fixture"):
1117511175
self.test_id = sb_config._test_id
11176+
if hasattr(sb_config, "_sb_pdb_driver"):
11177+
sb_config._sb_pdb_driver = None
1117611178
self.browser = sb_config.browser
1117711179
self.account = sb_config.account
1117811180
self.data = sb_config.data
@@ -11685,6 +11687,21 @@ def __add_pytest_html_extra(self):
1168511687
except Exception:
1168611688
pass
1168711689

11690+
def __delay_driver_quit(self):
11691+
delay_driver_quit = False
11692+
if (
11693+
hasattr(self, "_using_sb_fixture")
11694+
and self._using_sb_fixture
11695+
and "--pdb" in sys.argv
11696+
and self.__has_exception()
11697+
and len(self._drivers_list) == 1
11698+
and self.driver == self._default_driver
11699+
):
11700+
# Special case: Using sb fixture, --pdb, and has error.
11701+
# Keep the driver open for debugging and quit it later.
11702+
delay_driver_quit = True
11703+
return delay_driver_quit
11704+
1168811705
def __quit_all_drivers(self):
1168911706
if self._reuse_session and sb_config.shared_driver:
1169011707
if len(self._drivers_list) > 0:
@@ -11698,20 +11715,25 @@ def __quit_all_drivers(self):
1169811715
self._drivers_list = self._drivers_list[1:]
1169911716
else:
1170011717
self._drivers_list = []
11701-
1170211718
# Close all open browser windows
11719+
delay_driver_quit = self.__delay_driver_quit()
1170311720
self._drivers_list.reverse() # Last In, First Out
1170411721
for driver in self._drivers_list:
1170511722
try:
1170611723
if not is_windows or driver.service.process:
11707-
driver.quit()
11724+
if not delay_driver_quit:
11725+
driver.quit()
11726+
else:
11727+
# Save it for later to quit it later.
11728+
sb_config._sb_pdb_driver = driver
1170811729
except AttributeError:
1170911730
pass
1171011731
except Exception:
1171111732
pass
11712-
self.driver = None
11713-
self._default_driver = None
11714-
self._drivers_list = []
11733+
if not delay_driver_quit:
11734+
self.driver = None
11735+
self._default_driver = None
11736+
self._drivers_list = []
1171511737

1171611738
def __has_exception(self):
1171711739
has_exception = False

seleniumbase/plugins/pytest_plugin.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,17 +1345,32 @@ def pytest_runtest_teardown(item):
13451345
Make sure that webdriver and headless displays have exited.
13461346
(Has zero effect on tests using --reuse-session / --rs)"""
13471347
try:
1348-
self = item._testcase
1349-
try:
1350-
if (
1351-
hasattr(self, "driver")
1352-
and self.driver
1353-
and "--pdb" not in sys.argv
1348+
if hasattr(item, "_testcase") or hasattr(sb_config, "_sb_pdb_driver"):
1349+
if hasattr(item, "_testcase"):
1350+
self = item._testcase
1351+
try:
1352+
if (
1353+
hasattr(self, "driver")
1354+
and self.driver
1355+
and "--pdb" not in sys.argv
1356+
):
1357+
if not is_windows or self.driver.service.process:
1358+
self.driver.quit()
1359+
except Exception:
1360+
pass
1361+
elif (
1362+
hasattr(sb_config, "_sb_pdb_driver")
1363+
and sb_config._sb_pdb_driver
13541364
):
1355-
if not is_windows or self.driver.service.process:
1356-
self.driver.quit()
1357-
except Exception:
1358-
pass
1365+
try:
1366+
if (
1367+
not is_windows
1368+
or sb_config._sb_pdb_driver.service.process
1369+
):
1370+
sb_config._sb_pdb_driver.quit()
1371+
sb_config._sb_pdb_driver = None
1372+
except Exception:
1373+
pass
13591374
try:
13601375
if hasattr(self, "xvfb") and self.xvfb:
13611376
if self.headless_active and "--pdb" not in sys.argv:

0 commit comments

Comments
 (0)