Skip to content

Commit e1190ad

Browse files
committed
Improved exception-handling
1 parent 84a132a commit e1190ad

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

seleniumbase/fixtures/page_actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def hover_element(driver, element):
114114

115115

116116
def timeout_exception(exception, message):
117-
message = s_utils.format_exc(exception, message)
118-
raise Exception(message)
117+
exc, message = s_utils.format_exc(exception, message)
118+
raise exc(message)
119119

120120

121121
def hover_and_click(driver, hover_selector, click_selector,

seleniumbase/fixtures/shared_utils.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from selenium.webdriver.remote.errorhandler import NoAlertPresentException
88
from selenium.webdriver.remote.errorhandler import NoSuchFrameException
99
from selenium.webdriver.remote.errorhandler import NoSuchWindowException
10+
from seleniumbase.common.exceptions import NoSuchFileException
11+
from seleniumbase.common.exceptions import TimeLimitExceededException
1012
from seleniumbase import config as sb_config
1113

1214

@@ -15,27 +17,48 @@ def format_exc(exception, message):
1517
Formats an exception message to make the output cleaner.
1618
"""
1719
if exception == Exception:
18-
pass
20+
exc = Exception
21+
return exc, message
1922
elif exception == ElementNotVisibleException:
20-
message = "ElementNotVisibleException: %s" % message
23+
exc = ElementNotVisibleException
24+
elif exception == "ElementNotVisibleException":
25+
exc = ElementNotVisibleException
2126
elif exception == NoSuchElementException:
22-
message = "NoSuchElementException: %s" % message
27+
exc = NoSuchElementException
28+
elif exception == "NoSuchElementException":
29+
exc = NoSuchElementException
2330
elif exception == NoAlertPresentException:
24-
message = "NoAlertPresentException: %s" % message
31+
exc = NoAlertPresentException
32+
elif exception == "NoAlertPresentException":
33+
exc = NoAlertPresentException
2534
elif exception == NoSuchFrameException:
26-
message = "NoSuchFrameException: %s" % message
35+
exc = NoSuchFrameException
36+
elif exception == "NoSuchFrameException":
37+
exc = NoSuchFrameException
2738
elif exception == NoSuchWindowException:
28-
message = "NoSuchWindowException: %s" % message
39+
exc = NoSuchWindowException
40+
elif exception == "NoSuchWindowException":
41+
exc = NoSuchWindowException
42+
elif exception == "NoSuchFileException":
43+
exc = NoSuchFileException
2944
elif type(exception) is str:
45+
exc = Exception
3046
message = "%s: %s" % (exception, message)
47+
return exc, message
3148
else:
32-
pass
49+
exc = Exception
50+
return exc, message
51+
message = _format_message(message)
52+
return exc, message
53+
54+
55+
def _format_message(message):
56+
message = "\n " + message
3357
return message
3458

3559

3660
def __time_limit_exceeded(message):
37-
raise Exception(
38-
"TimeLimitExceeded: %s" % message)
61+
raise TimeLimitExceededException(message)
3962

4063

4164
def check_if_time_limit_exceeded():
@@ -52,4 +75,5 @@ def check_if_time_limit_exceeded():
5275
message = (
5376
"This test has exceeded the time limit of %s second%s!"
5477
"" % (display_time_limit, plural))
78+
message = _format_message(message)
5579
__time_limit_exceeded(message)

0 commit comments

Comments
 (0)