Skip to content

Commit ef6fcf9

Browse files
committed
Add new method: quit_extra_driver()
1 parent c1b6d52 commit ef6fcf9

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9927,6 +9927,73 @@ def switch_to_alert(self, timeout=None):
99279927

99289928
############
99299929

9930+
def quit_extra_driver(self, driver=None):
9931+
""" Quits the driver only if it's not the default/initial driver.
9932+
If a driver is given, quits that, otherwise quits the active driver.
9933+
Raises an Exception if quitting the default/initial driver.
9934+
Should only be called if a test has already called get_new_driver().
9935+
Afterwards, self.driver points to the default/initial driver
9936+
if self.driver was the one being quit.
9937+
----
9938+
If a test never calls get_new_driver(), this method isn't needed.
9939+
SeleniumBase automatically quits browsers after tests have ended.
9940+
Even if tests do call get_new_driver(), you don't need to use this
9941+
method unless you want to quit extra browsers before a test ends.
9942+
----
9943+
Terminology and important details:
9944+
* Active driver: The one self.driver is set to. Used within methods.
9945+
* Default/initial driver: The one that is spun up when tests start.
9946+
Initially, the active driver and the default driver are the same.
9947+
The active driver can change when one of these methods is called:
9948+
> self.get_new_driver()
9949+
> self.switch_to_default_driver()
9950+
> self.switch_to_driver()
9951+
> self.quit_extra_driver()
9952+
"""
9953+
self.__check_scope()
9954+
if not driver:
9955+
driver = self.driver
9956+
if type(driver).__name__ == "NoneType":
9957+
raise Exception("The driver to quit was a NoneType variable!")
9958+
elif (
9959+
not hasattr(driver, "get")
9960+
or not hasattr(driver, "name")
9961+
or not hasattr(driver, "quit")
9962+
or not hasattr(driver, "capabilities")
9963+
or not hasattr(driver, "window_handles")
9964+
):
9965+
raise Exception("The driver to quit does not match a Driver!")
9966+
elif self._reuse_session and driver == self._default_driver:
9967+
raise Exception(
9968+
"Cannot quit the initial driver in --reuse-session mode!\n"
9969+
"This is done automatically after all tests have ended.\n"
9970+
"Use this method only if get_new_driver() has been called."
9971+
)
9972+
elif (
9973+
driver == self._default_driver
9974+
or (driver in self._drivers_list and len(self._drivers_list) == 1)
9975+
):
9976+
raise Exception(
9977+
"Cannot quit the default/initial driver!\n"
9978+
"This is done automatically at the end of each test.\n"
9979+
"Use this method only if get_new_driver() has been called."
9980+
)
9981+
try:
9982+
driver.quit()
9983+
except AttributeError:
9984+
pass
9985+
except Exception:
9986+
pass
9987+
if driver in self._drivers_list:
9988+
self._drivers_list.remove(driver)
9989+
if driver in self._drivers_browser_map:
9990+
del self._drivers_browser_map[driver]
9991+
# If the driver to quit was the active driver, switch drivers
9992+
if driver == self.driver:
9993+
self.switch_to_default_driver()
9994+
9995+
############
9996+
99309997
def __assert_eq(self, *args, **kwargs):
99319998
""" Minified assert_equal() using only the list diff. """
99329999
minified_exception = None

0 commit comments

Comments
 (0)