Improve error-handling

This commit is contained in:
Michael Mintz 2020-08-14 20:30:55 -04:00
parent db1923afc3
commit f26bdeac39
2 changed files with 44 additions and 33 deletions

View File

@ -97,6 +97,14 @@ class BaseCase(unittest.TestCase):
def open(self, url): def open(self, url):
""" Navigates the current browser window to the specified page. """ """ Navigates the current browser window to the specified page. """
if type(url) is str:
url = url.strip() # Remove leading and trailing whitespace
if (type(url) is not str) or not self.__looks_like_a_page_url(url):
# url should start with one of the following:
# "http:", "https:", "://", "data:", "file:",
# "about:", "chrome:", "opera:", or "edge:".
msg = 'Did you forget to prefix your URL with "http:" or "https:"?'
raise Exception('Invalid URL: "%s"\n%s' % (url, msg))
self.__last_page_load_url = None self.__last_page_load_url = None
js_utils.clear_out_console_logs(self.driver) js_utils.clear_out_console_logs(self.driver)
if url.startswith("://"): if url.startswith("://"):
@ -5797,9 +5805,10 @@ class BaseCase(unittest.TestCase):
navigate to the page if a URL is detected, but will instead call navigate to the page if a URL is detected, but will instead call
self.get_element(URL_AS_A_SELECTOR) if the input in not a URL. """ self.get_element(URL_AS_A_SELECTOR) if the input in not a URL. """
if (url.startswith("http:") or url.startswith("https:") or ( if (url.startswith("http:") or url.startswith("https:") or (
url.startswith("://") or url.startswith("data:") or ( url.startswith("://") or url.startswith("chrome:") or (
url.startswith("about:") or url.startswith("chrome:") or ( url.startswith("about:") or url.startswith("data:") or (
url.startswith("file:"))))): url.startswith("file:") or url.startswith("edge:") or (
url.startswith("opera:")))))):
return True return True
else: else:
return False return False
@ -6367,6 +6376,34 @@ class BaseCase(unittest.TestCase):
You'll need to add the following line to the subclass's tearDown(): You'll need to add the following line to the subclass's tearDown():
super(SubClassOfBaseCase, self).tearDown() super(SubClassOfBaseCase, self).tearDown()
""" """
try:
with_selenium = self.with_selenium
except Exception:
sub_class_name = str(
self.__class__.__bases__[0]).split('.')[-1].split("'")[0]
sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]
sub_file_name = sub_file_name + ".py"
class_name = str(self.__class__).split('.')[-1].split("'")[0]
file_name = str(self.__class__).split('.')[-2] + ".py"
class_name_used = sub_class_name
file_name_used = sub_file_name
if sub_class_name == "BaseCase":
class_name_used = class_name
file_name_used = file_name
fix_setup = "super(%s, self).setUp()" % class_name_used
fix_teardown = "super(%s, self).tearDown()" % class_name_used
message = ("You're overriding SeleniumBase's BaseCase setUp() "
"method with your own setUp() method, which breaks "
"SeleniumBase. You can fix this by going to your "
"%s class located in your %s file and adding the "
"following line of code AT THE BEGINNING of your "
"setUp() method:\n%s\n\nAlso make sure "
"you have added the following line of code AT THE "
"END of your tearDown() method:\n%s\n"
% (class_name_used, file_name_used,
fix_setup, fix_teardown))
raise Exception(message)
# *** Start tearDown() officially ***
self.__slow_mode_pause_if_active() self.__slow_mode_pause_if_active()
has_exception = self.__has_exception() has_exception = self.__has_exception()
if self.__deferred_assert_failures: if self.__deferred_assert_failures:
@ -6381,33 +6418,6 @@ class BaseCase(unittest.TestCase):
if self.is_pytest: if self.is_pytest:
# pytest-specific code # pytest-specific code
test_id = self.__get_test_id() test_id = self.__get_test_id()
try:
with_selenium = self.with_selenium
except Exception:
sub_class_name = str(
self.__class__.__bases__[0]).split('.')[-1].split("'")[0]
sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]
sub_file_name = sub_file_name + ".py"
class_name = str(self.__class__).split('.')[-1].split("'")[0]
file_name = str(self.__class__).split('.')[-2] + ".py"
class_name_used = sub_class_name
file_name_used = sub_file_name
if sub_class_name == "BaseCase":
class_name_used = class_name
file_name_used = file_name
fix_setup = "super(%s, self).setUp()" % class_name_used
fix_teardown = "super(%s, self).tearDown()" % class_name_used
message = ("You're overriding SeleniumBase's BaseCase setUp() "
"method with your own setUp() method, which breaks "
"SeleniumBase. You can fix this by going to your "
"%s class located in your %s file and adding the "
"following line of code AT THE BEGINNING of your "
"setUp() method:\n%s\n\nAlso make sure "
"you have added the following line of code AT THE "
"END of your tearDown() method:\n%s\n"
% (class_name_used, file_name_used,
fix_setup, fix_teardown))
raise Exception(message)
if with_selenium: if with_selenium:
# Save a screenshot if logging is on when an exception occurs # Save a screenshot if logging is on when an exception occurs
if has_exception: if has_exception:

View File

@ -106,9 +106,10 @@ def is_valid_url(url):
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE) r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if regex.match(url) or ((url.startswith('about:') or ( if regex.match(url) or ((url.startswith("about:") or (
url.startswith('data:') or url.startswith('chrome:'))) url.startswith("data:") or url.startswith("chrome:") or (
and " " not in url): url.startswith("edge:") or url.startswith("opera:") or (
url.startswith("file:")))))):
return True return True
else: else:
return False return False