Skip to content

Commit 6941812

Browse files
committed
Improve handling of exceptions
1 parent 1d90bc1 commit 6941812

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

seleniumbase/common/decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import warnings
55
from contextlib import contextmanager
66
from functools import wraps
7+
from seleniumbase.common.exceptions import TimeoutException
78

89

910
@contextmanager
@@ -62,7 +63,7 @@ def my_method():
6263
)
6364
if exception:
6465
message = exception.msg + "\nAND " + message
65-
raise Exception(message)
66+
raise TimeoutException(message)
6667

6768

6869
@contextmanager
@@ -108,7 +109,7 @@ def my_method():
108109
)
109110
if exception:
110111
message = exception.msg + "\nAND " + message
111-
raise Exception(message)
112+
raise TimeoutException(message)
112113

113114

114115
def retry_on_exception(tries=6, delay=1, backoff=2, max_delay=32):

seleniumbase/common/exceptions.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
OutOfScopeException => Used by BaseCase methods when setUp() is skipped.
77
TextNotVisibleException => Called when expected text fails to appear.
88
TimeLimitExceededException => Called when exceeding "--time-limit=SECONDS".
9+
TimeoutException => Called when some timeout limit has been exceeded.
910
VisualException => Called when there's a Visual Diff Assertion Failure.
1011
"""
1112

@@ -38,5 +39,64 @@ class TimeLimitExceededException(Exception):
3839
pass
3940

4041

42+
class TimeoutException(Exception):
43+
pass
44+
45+
4146
class VisualException(Exception):
4247
pass
48+
49+
50+
""" Selenium Exceptions (Simplified for SeleniumBase) """
51+
52+
53+
class WebDriverException(Exception):
54+
"""Base webdriver exception."""
55+
56+
def __init__(self, msg=None, screen=None, stacktrace=None):
57+
super().__init__()
58+
self.msg = msg
59+
self.screen = screen
60+
self.stacktrace = stacktrace
61+
62+
def __str__(self):
63+
exception_msg = "Message: %s\n" % self.msg
64+
if self.screen:
65+
exception_msg += "Screenshot: available via screen\n"
66+
if self.stacktrace:
67+
stacktrace = "\n".join(self.stacktrace)
68+
exception_msg += "Stacktrace:\n%s" % stacktrace
69+
return exception_msg
70+
71+
72+
class InvalidSwitchToTargetException(WebDriverException):
73+
"""Thrown when frame or window target to be switched doesn't exist."""
74+
75+
76+
class NoSuchFrameException(InvalidSwitchToTargetException):
77+
"""Thrown when frame target to be switched doesn't exist."""
78+
79+
80+
class NoSuchWindowException(InvalidSwitchToTargetException):
81+
"""Thrown when window target to be switched doesn't exist."""
82+
83+
84+
class NoSuchElementException(WebDriverException):
85+
"""Thrown when element could not be found."""
86+
87+
88+
class NoSuchAttributeException(WebDriverException):
89+
"""Thrown when the attribute of element could not be found."""
90+
91+
92+
class InvalidElementStateException(WebDriverException):
93+
"""Thrown when a command could not be completed because the element is in
94+
an invalid state."""
95+
96+
97+
class NoAlertPresentException(WebDriverException):
98+
"""Thrown when switching to no presented alert."""
99+
100+
101+
class ElementNotVisibleException(InvalidElementStateException):
102+
"""Thrown when an element is present in the DOM, but not visible."""

seleniumbase/fixtures/shared_utils.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,42 +124,43 @@ def format_exc(exception, message):
124124
from selenium.common.exceptions import NoSuchWindowException
125125
from seleniumbase.common.exceptions import NoSuchFileException
126126
from seleniumbase.common.exceptions import TextNotVisibleException
127+
from seleniumbase.common import exceptions
127128

128129
if exception == Exception:
129130
exc = Exception
130131
return exc, message
131132
elif exception == ElementNotVisibleException:
132-
exc = ElementNotVisibleException
133+
exc = exceptions.ElementNotVisibleException
133134
elif exception == "ElementNotVisibleException":
134-
exc = ElementNotVisibleException
135+
exc = exceptions.ElementNotVisibleException
135136
elif exception == NoSuchElementException:
136-
exc = NoSuchElementException
137+
exc = exceptions.NoSuchElementException
137138
elif exception == "NoSuchElementException":
138-
exc = NoSuchElementException
139+
exc = exceptions.NoSuchElementException
139140
elif exception == TextNotVisibleException:
140-
exc = TextNotVisibleException
141+
exc = exceptions.TextNotVisibleException
141142
elif exception == "TextNotVisibleException":
142-
exc = TextNotVisibleException
143+
exc = exceptions.TextNotVisibleException
143144
elif exception == NoAlertPresentException:
144-
exc = NoAlertPresentException
145+
exc = exceptions.NoAlertPresentException
145146
elif exception == "NoAlertPresentException":
146-
exc = NoAlertPresentException
147+
exc = exceptions.NoAlertPresentException
147148
elif exception == NoSuchAttributeException:
148-
exc = NoSuchAttributeException
149+
exc = exceptions.NoSuchAttributeException
149150
elif exception == "NoSuchAttributeException":
150-
exc = NoSuchAttributeException
151+
exc = exceptions.NoSuchAttributeException
151152
elif exception == NoSuchFrameException:
152-
exc = NoSuchFrameException
153+
exc = exceptions.NoSuchFrameException
153154
elif exception == "NoSuchFrameException":
154-
exc = NoSuchFrameException
155+
exc = exceptions.NoSuchFrameException
155156
elif exception == NoSuchWindowException:
156-
exc = NoSuchWindowException
157+
exc = exceptions.NoSuchWindowException
157158
elif exception == "NoSuchWindowException":
158-
exc = NoSuchWindowException
159+
exc = exceptions.NoSuchWindowException
159160
elif exception == NoSuchFileException:
160-
exc = NoSuchFileException
161+
exc = exceptions.NoSuchFileException
161162
elif exception == "NoSuchFileException":
162-
exc = NoSuchFileException
163+
exc = exceptions.NoSuchFileException
163164
elif type(exception) is str:
164165
exc = Exception
165166
message = "%s: %s" % (exception, message)

0 commit comments

Comments
 (0)