Skip to content

Commit 91415a2

Browse files
committed
Refactoring code for different versions of Python
1 parent d84f11e commit 91415a2

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def test_anything(self):
6666
logging.getLogger("urllib3").setLevel(logging.ERROR)
6767
urllib3.disable_warnings()
6868
LOGGER.setLevel(logging.WARNING)
69+
python3 = True
6970
if sys.version_info[0] < 3:
71+
python3 = False
7072
reload(sys) # noqa: F821
7173
sys.setdefaultencoding("utf8")
7274
selenium4 = False
@@ -144,9 +146,8 @@ def open(self, url):
144146
pre_action_url = self.driver.current_url
145147
except Exception:
146148
pass
147-
if type(url) is str:
148-
url = url.strip() # Remove leading and trailing whitespace
149-
if (type(url) is not str) or not self.__looks_like_a_page_url(url):
149+
url = str(url).strip() # Remove leading and trailing whitespace
150+
if not self.__looks_like_a_page_url(url):
150151
# url should start with one of the following:
151152
# "http:", "https:", "://", "data:", "file:",
152153
# "about:", "chrome:", "opera:", or "edge:".
@@ -742,7 +743,7 @@ def refresh(self):
742743
def get_current_url(self):
743744
self.__check_scope()
744745
current_url = self.driver.current_url
745-
if "%" in current_url and sys.version_info[0] >= 3:
746+
if "%" in current_url and python3:
746747
try:
747748
from urllib.parse import unquote
748749

@@ -2386,6 +2387,8 @@ def open_html_file(self, html_file):
23862387
def execute_script(self, script, *args, **kwargs):
23872388
self.__check_scope()
23882389
self.__check_browser()
2390+
if not python3:
2391+
script = unicode(script.decode('latin-1')) # noqa: F821
23892392
return self.driver.execute_script(script, *args, **kwargs)
23902393

23912394
def execute_async_script(self, script, timeout=None):
@@ -2520,6 +2523,8 @@ def set_content_to_frame(self, frame, timeout=None):
25202523
url = self.execute_script(
25212524
"""return document.querySelector('%s').src;""" % frame
25222525
)
2526+
if not python3:
2527+
url = str(url)
25232528
if url and len(url) > 0:
25242529
if ("http:") in url or ("https:") in url or ("file:") in url:
25252530
pass
@@ -3547,7 +3552,7 @@ def __process_recorded_actions(self):
35473552
cleaned_actions.append(srt_actions[n])
35483553
for action in srt_actions:
35493554
if action[0] == "begin" or action[0] == "_url_":
3550-
if "%" in action[2] and sys.version_info[0] >= 3:
3555+
if "%" in action[2] and python3:
35513556
try:
35523557
from urllib.parse import unquote
35533558

@@ -3556,7 +3561,7 @@ def __process_recorded_actions(self):
35563561
pass
35573562
sb_actions.append('self.open("%s")' % action[2])
35583563
elif action[0] == "f_url":
3559-
if "%" in action[2] and sys.version_info[0] >= 3:
3564+
if "%" in action[2] and python3:
35603565
try:
35613566
from urllib.parse import unquote
35623567

@@ -10464,7 +10469,7 @@ def __recalculate_selector(self, selector, by, xp_ok=True):
1046410469
used to make the ":contains()" selector valid outside JS calls."""
1046510470
_type = type(selector) # First make sure the selector is a string
1046610471
not_string = False
10467-
if sys.version_info[0] < 3:
10472+
if not python3:
1046810473
if _type is not str and _type is not unicode: # noqa: F821
1046910474
not_string = True
1047010475
else:
@@ -11123,7 +11128,7 @@ def __set_last_page_source(self):
1112311128
def __get_exception_info(self):
1112411129
exc_message = None
1112511130
if (
11126-
sys.version_info[0] >= 3
11131+
python3
1112711132
and hasattr(self, "_outcome")
1112811133
and (hasattr(self._outcome, "errors") and self._outcome.errors)
1112911134
):
@@ -11234,11 +11239,11 @@ def __has_exception(self):
1123411239
has_exception = False
1123511240
if hasattr(sys, "last_traceback") and sys.last_traceback is not None:
1123611241
has_exception = True
11237-
elif sys.version_info[0] >= 3 and hasattr(self, "_outcome"):
11242+
elif python3 and hasattr(self, "_outcome"):
1123811243
if hasattr(self._outcome, "errors") and self._outcome.errors:
1123911244
has_exception = True
1124011245
else:
11241-
if sys.version_info[0] >= 3:
11246+
if python3:
1124211247
has_exception = sys.exc_info()[1] is not None
1124311248
else:
1124411249
if not hasattr(self, "_using_sb_fixture_class") and (
@@ -11247,7 +11252,10 @@ def __has_exception(self):
1124711252
has_exception = sys.exc_info()[1] is not None
1124811253
else:
1124911254
has_exception = len(str(sys.exc_info()[1]).strip()) > 0
11250-
if hasattr(self, "_using_sb_fixture") and self.__will_be_skipped:
11255+
if (
11256+
self.__will_be_skipped
11257+
and (hasattr(self, "_using_sb_fixture") or not python3)
11258+
):
1125111259
has_exception = False
1125211260
return has_exception
1125311261

0 commit comments

Comments
 (0)