Skip to content

Commit f26bdea

Browse files
committed
Improve error-handling
1 parent db1923a commit f26bdea

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ def __init__(self, *args, **kwargs):
9797

9898
def open(self, url):
9999
""" Navigates the current browser window to the specified page. """
100+
if type(url) is str:
101+
url = url.strip() # Remove leading and trailing whitespace
102+
if (type(url) is not str) or not self.__looks_like_a_page_url(url):
103+
# url should start with one of the following:
104+
# "http:", "https:", "://", "data:", "file:",
105+
# "about:", "chrome:", "opera:", or "edge:".
106+
msg = 'Did you forget to prefix your URL with "http:" or "https:"?'
107+
raise Exception('Invalid URL: "%s"\n%s' % (url, msg))
100108
self.__last_page_load_url = None
101109
js_utils.clear_out_console_logs(self.driver)
102110
if url.startswith("://"):
@@ -5797,9 +5805,10 @@ def __looks_like_a_page_url(self, url):
57975805
navigate to the page if a URL is detected, but will instead call
57985806
self.get_element(URL_AS_A_SELECTOR) if the input in not a URL. """
57995807
if (url.startswith("http:") or url.startswith("https:") or (
5800-
url.startswith("://") or url.startswith("data:") or (
5801-
url.startswith("about:") or url.startswith("chrome:") or (
5802-
url.startswith("file:"))))):
5808+
url.startswith("://") or url.startswith("chrome:") or (
5809+
url.startswith("about:") or url.startswith("data:") or (
5810+
url.startswith("file:") or url.startswith("edge:") or (
5811+
url.startswith("opera:")))))):
58035812
return True
58045813
else:
58055814
return False
@@ -6367,6 +6376,34 @@ def tearDown(self):
63676376
You'll need to add the following line to the subclass's tearDown():
63686377
super(SubClassOfBaseCase, self).tearDown()
63696378
"""
6379+
try:
6380+
with_selenium = self.with_selenium
6381+
except Exception:
6382+
sub_class_name = str(
6383+
self.__class__.__bases__[0]).split('.')[-1].split("'")[0]
6384+
sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]
6385+
sub_file_name = sub_file_name + ".py"
6386+
class_name = str(self.__class__).split('.')[-1].split("'")[0]
6387+
file_name = str(self.__class__).split('.')[-2] + ".py"
6388+
class_name_used = sub_class_name
6389+
file_name_used = sub_file_name
6390+
if sub_class_name == "BaseCase":
6391+
class_name_used = class_name
6392+
file_name_used = file_name
6393+
fix_setup = "super(%s, self).setUp()" % class_name_used
6394+
fix_teardown = "super(%s, self).tearDown()" % class_name_used
6395+
message = ("You're overriding SeleniumBase's BaseCase setUp() "
6396+
"method with your own setUp() method, which breaks "
6397+
"SeleniumBase. You can fix this by going to your "
6398+
"%s class located in your %s file and adding the "
6399+
"following line of code AT THE BEGINNING of your "
6400+
"setUp() method:\n%s\n\nAlso make sure "
6401+
"you have added the following line of code AT THE "
6402+
"END of your tearDown() method:\n%s\n"
6403+
% (class_name_used, file_name_used,
6404+
fix_setup, fix_teardown))
6405+
raise Exception(message)
6406+
# *** Start tearDown() officially ***
63706407
self.__slow_mode_pause_if_active()
63716408
has_exception = self.__has_exception()
63726409
if self.__deferred_assert_failures:
@@ -6381,33 +6418,6 @@ def tearDown(self):
63816418
if self.is_pytest:
63826419
# pytest-specific code
63836420
test_id = self.__get_test_id()
6384-
try:
6385-
with_selenium = self.with_selenium
6386-
except Exception:
6387-
sub_class_name = str(
6388-
self.__class__.__bases__[0]).split('.')[-1].split("'")[0]
6389-
sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]
6390-
sub_file_name = sub_file_name + ".py"
6391-
class_name = str(self.__class__).split('.')[-1].split("'")[0]
6392-
file_name = str(self.__class__).split('.')[-2] + ".py"
6393-
class_name_used = sub_class_name
6394-
file_name_used = sub_file_name
6395-
if sub_class_name == "BaseCase":
6396-
class_name_used = class_name
6397-
file_name_used = file_name
6398-
fix_setup = "super(%s, self).setUp()" % class_name_used
6399-
fix_teardown = "super(%s, self).tearDown()" % class_name_used
6400-
message = ("You're overriding SeleniumBase's BaseCase setUp() "
6401-
"method with your own setUp() method, which breaks "
6402-
"SeleniumBase. You can fix this by going to your "
6403-
"%s class located in your %s file and adding the "
6404-
"following line of code AT THE BEGINNING of your "
6405-
"setUp() method:\n%s\n\nAlso make sure "
6406-
"you have added the following line of code AT THE "
6407-
"END of your tearDown() method:\n%s\n"
6408-
% (class_name_used, file_name_used,
6409-
fix_setup, fix_teardown))
6410-
raise Exception(message)
64116421
if with_selenium:
64126422
# Save a screenshot if logging is on when an exception occurs
64136423
if has_exception:

seleniumbase/fixtures/page_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ def is_valid_url(url):
106106
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
107107
r'(?::\d+)?' # optional port
108108
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
109-
if regex.match(url) or ((url.startswith('about:') or (
110-
url.startswith('data:') or url.startswith('chrome:')))
111-
and " " not in url):
109+
if regex.match(url) or ((url.startswith("about:") or (
110+
url.startswith("data:") or url.startswith("chrome:") or (
111+
url.startswith("edge:") or url.startswith("opera:") or (
112+
url.startswith("file:")))))):
112113
return True
113114
else:
114115
return False

0 commit comments

Comments
 (0)