Skip to content

Commit 2514147

Browse files
authored
Merge pull request #1253 from seleniumbase/new-method-dependencies-and-refactoring
New method / refresh dependencies / some refactoring
2 parents fbadbdd + ea3f2a9 commit 2514147

File tree

5 files changed

+103
-17
lines changed

5 files changed

+103
-17
lines changed

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ self.switch_to_alert(timeout=None)
723723

724724
############
725725

726+
self.quit_extra_driver(driver=None)
727+
728+
############
729+
726730
self.check_window(
727731
name="default", level=0, baseline=False, check_domain=True, full_diff=False)
728732

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ packaging>=21.3;python_version>="3.6"
66
setuptools>=44.1.1;python_version<"3.5"
77
setuptools>=50.3.2;python_version>="3.5" and python_version<"3.6"
88
setuptools>=59.6.0;python_version>="3.6" and python_version<"3.7"
9-
setuptools>=60.9.3;python_version>="3.7"
9+
setuptools>=60.10.0;python_version>="3.7"
1010
setuptools-scm>=5.0.2;python_version<"3.6"
1111
setuptools-scm>=6.4.2;python_version>="3.6"
1212
tomli>=1.2.3;python_version>="3.6" and python_version<"3.7"
@@ -35,7 +35,7 @@ idna==3.3;python_version>="3.6"
3535
chardet==3.0.4;python_version<"3.5"
3636
chardet==4.0.0;python_version>="3.5"
3737
charset-normalizer==2.0.12;python_version>="3.5"
38-
urllib3==1.26.8
38+
urllib3==1.26.9
3939
requests==2.27.1;python_version<"3.5"
4040
requests==2.25.1;python_version>="3.5" and python_version<"3.6"
4141
requests==2.27.1;python_version>="3.6"
@@ -64,7 +64,7 @@ py==1.11.0;python_version>="3.5"
6464
pytest==4.6.11;python_version<"3.5"
6565
pytest==6.1.2;python_version>="3.5" and python_version<"3.6"
6666
pytest==7.0.1;python_version>="3.6" and python_version<"3.7"
67-
pytest==7.1.0;python_version>="3.7"
67+
pytest==7.1.1;python_version>="3.7"
6868
pytest-forked==1.3.0;python_version<"3.6"
6969
pytest-forked==1.4.0;python_version>="3.6"
7070
pytest-html==1.22.1;python_version<"3.6"

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "2.4.21"
2+
__version__ = "2.4.22"

seleniumbase/fixtures/base_case.py

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def __init__(self, *args, **kwargs):
117117
self.__device_width = None
118118
self.__device_height = None
119119
self.__device_pixel_ratio = None
120-
self.__driver_browser_map = {}
121120
self.__changed_jqc_theme = False
122121
self.__jqc_default_theme = None
123122
self.__jqc_default_color = None
@@ -131,6 +130,7 @@ def __init__(self, *args, **kwargs):
131130
self._html_report_extra = [] # (Used by pytest_plugin.py)
132131
self._default_driver = None
133132
self._drivers_list = []
133+
self._drivers_browser_map = {}
134134
self._chart_data = {}
135135
self._chart_count = 0
136136
self._chart_label = {}
@@ -1028,7 +1028,11 @@ def click_link_text(self, link_text, timeout=None):
10281028
self.__demo_mode_highlight_if_active(link_text, by=By.LINK_TEXT)
10291029
try:
10301030
element.click()
1031-
except (StaleElementReferenceException, ENI_Exception):
1031+
except (
1032+
StaleElementReferenceException,
1033+
ENI_Exception,
1034+
ECI_Exception,
1035+
):
10321036
self.wait_for_ready_state_complete()
10331037
time.sleep(0.16)
10341038
element = self.wait_for_link_text_visible(
@@ -1153,7 +1157,11 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
11531157
)
11541158
try:
11551159
element.click()
1156-
except (StaleElementReferenceException, ENI_Exception):
1160+
except (
1161+
StaleElementReferenceException,
1162+
ENI_Exception,
1163+
ECI_Exception,
1164+
):
11571165
self.wait_for_ready_state_complete()
11581166
time.sleep(0.16)
11591167
element = self.wait_for_partial_link_text(
@@ -1665,7 +1673,7 @@ def click_nth_visible_element(
16651673
try:
16661674
self.__scroll_to_element(element)
16671675
element.click()
1668-
except (StaleElementReferenceException, ENI_Exception):
1676+
except (StaleElementReferenceException, ENI_Exception, ECI_Exception):
16691677
time.sleep(0.12)
16701678
self.wait_for_ready_state_complete()
16711679
self.wait_for_element_present(selector, by=by, timeout=timeout)
@@ -3043,7 +3051,7 @@ def get_new_driver(
30433051
device_pixel_ratio=d_p_r,
30443052
)
30453053
self._drivers_list.append(new_driver)
3046-
self.__driver_browser_map[new_driver] = browser_name
3054+
self._drivers_browser_map[new_driver] = browser_name
30473055
if switch_to:
30483056
self.driver = new_driver
30493057
self.browser = browser_name
@@ -3124,16 +3132,16 @@ def switch_to_driver(self, driver):
31243132
You may need this if using self.get_new_driver() in your code."""
31253133
self.__check_scope()
31263134
self.driver = driver
3127-
if self.driver in self.__driver_browser_map:
3128-
self.browser = self.__driver_browser_map[self.driver]
3135+
if self.driver in self._drivers_browser_map:
3136+
self.browser = self._drivers_browser_map[self.driver]
31293137
self.bring_active_window_to_front()
31303138

31313139
def switch_to_default_driver(self):
3132-
""" Sets self.driver to the default/original driver. """
3140+
""" Sets self.driver to the default/initial driver. """
31333141
self.__check_scope()
31343142
self.driver = self._default_driver
3135-
if self.driver in self.__driver_browser_map:
3136-
self.browser = self.__driver_browser_map[self.driver]
3143+
if self.driver in self._drivers_browser_map:
3144+
self.browser = self._drivers_browser_map[self.driver]
31373145
self.bring_active_window_to_front()
31383146

31393147
def save_screenshot(
@@ -9919,6 +9927,73 @@ def switch_to_alert(self, timeout=None):
99199927

99209928
############
99219929

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+
99229997
def __assert_eq(self, *args, **kwargs):
99239998
""" Minified assert_equal() using only the list diff. """
99249999
minified_exception = None
@@ -11410,6 +11485,11 @@ def setUp(self, masterqa_mode=False):
1141011485
self._default_driver = self.driver
1141111486
if self._reuse_session:
1141211487
sb_config.shared_driver = self.driver
11488+
if len(self._drivers_list) == 0:
11489+
# The user is overriding self.get_new_driver()
11490+
# (Otherwise this code shouldn't be reachable)
11491+
self._drivers_list.append(self.driver)
11492+
self._drivers_browser_map[self.driver] = self.browser
1141311493

1141411494
if self.browser in ["firefox", "ie", "safari", "opera"]:
1141511495
# Only Chrome and Edge browsers have the mobile emulator.

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
'setuptools>=44.1.1;python_version<"3.5"',
132132
'setuptools>=50.3.2;python_version>="3.5" and python_version<"3.6"',
133133
'setuptools>=59.6.0;python_version>="3.6" and python_version<"3.7"',
134-
'setuptools>=60.9.3;python_version>="3.7"',
134+
'setuptools>=60.10.0;python_version>="3.7"',
135135
'setuptools-scm>=5.0.2;python_version<"3.6"',
136136
'setuptools-scm>=6.4.2;python_version>="3.6"',
137137
'tomli>=1.2.3;python_version>="3.6" and python_version<"3.7"',
@@ -160,7 +160,7 @@
160160
'chardet==3.0.4;python_version<"3.5"', # Stay in sync with "requests"
161161
'chardet==4.0.0;python_version>="3.5"', # Stay in sync with "requests"
162162
'charset-normalizer==2.0.12;python_version>="3.5"', # Sync "requests"
163-
"urllib3==1.26.8", # Must stay in sync with "requests"
163+
"urllib3==1.26.9", # Must stay in sync with "requests"
164164
'requests==2.27.1;python_version<"3.5"',
165165
'requests==2.25.1;python_version>="3.5" and python_version<"3.6"',
166166
'requests==2.27.1;python_version>="3.6"',
@@ -189,7 +189,7 @@
189189
'pytest==4.6.11;python_version<"3.5"',
190190
'pytest==6.1.2;python_version>="3.5" and python_version<"3.6"',
191191
'pytest==7.0.1;python_version>="3.6" and python_version<"3.7"',
192-
'pytest==7.1.0;python_version>="3.7"',
192+
'pytest==7.1.1;python_version>="3.7"',
193193
'pytest-forked==1.3.0;python_version<"3.6"',
194194
'pytest-forked==1.4.0;python_version>="3.6"',
195195
'pytest-html==1.22.1;python_version<"3.6"',
@@ -251,6 +251,7 @@
251251
],
252252
extras_require={
253253
# pip install -e .[coverage]
254+
# Usage: coverage run -m pytest; coverage html; coverage report
254255
"coverage": [
255256
'coverage==5.5;python_version<"3.6"',
256257
'coverage==6.2;python_version>="3.6" and python_version<"3.7"',
@@ -259,6 +260,7 @@
259260
'pytest-cov==3.0.0;python_version>="3.6"',
260261
],
261262
# pip install -e .[flake]
263+
# Usage: flake8
262264
"flake": [
263265
'flake8==3.7.9;python_version<"3.5"',
264266
'flake8==3.9.2;python_version>="3.5" and python_version<"3.6"',

0 commit comments

Comments
 (0)