Skip to content

Commit

Permalink
[py]: tidy exceptions.py code
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Oct 1, 2022
1 parent fe9444d commit eaa7ecc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 72 deletions.
68 changes: 35 additions & 33 deletions py/selenium/common/__init__.py
Expand Up @@ -49,36 +49,38 @@
from .exceptions import UnknownMethodException
from .exceptions import WebDriverException

__all__ = ["WebDriverException",
"InvalidSwitchToTargetException",
"NoSuchFrameException",
"NoSuchWindowException",
"NoSuchElementException",
"NoSuchAttributeException",
"NoSuchShadowRootException",
"StaleElementReferenceException",
"InvalidElementStateException",
"UnexpectedAlertPresentException",
"NoAlertPresentException",
"ElementNotVisibleException",
"ElementNotInteractableException",
"ElementNotSelectableException",
"InvalidCookieDomainException",
"UnableToSetCookieException",
"RemoteDriverServerException",
"TimeoutException",
"MoveTargetOutOfBoundsException",
"UnexpectedTagNameException",
"InvalidSelectorException",
"ImeNotAvailableException",
"ImeActivationFailedException",
"InvalidArgumentException",
"JavascriptException",
"NoSuchCookieException",
"ScreenshotException",
"ElementClickInterceptedException",
"InsecureCertificateException",
"InvalidCoordinatesException",
"InvalidSessionIdException",
"SessionNotCreatedException",
"UnknownMethodException"]
__all__ = [
"WebDriverException",
"InvalidSwitchToTargetException",
"NoSuchFrameException",
"NoSuchWindowException",
"NoSuchElementException",
"NoSuchAttributeException",
"NoSuchShadowRootException",
"StaleElementReferenceException",
"InvalidElementStateException",
"UnexpectedAlertPresentException",
"NoAlertPresentException",
"ElementNotVisibleException",
"ElementNotInteractableException",
"ElementNotSelectableException",
"InvalidCookieDomainException",
"UnableToSetCookieException",
"RemoteDriverServerException",
"TimeoutException",
"MoveTargetOutOfBoundsException",
"UnexpectedTagNameException",
"InvalidSelectorException",
"ImeNotAvailableException",
"ImeActivationFailedException",
"InvalidArgumentException",
"JavascriptException",
"NoSuchCookieException",
"ScreenshotException",
"ElementClickInterceptedException",
"InsecureCertificateException",
"InvalidCoordinatesException",
"InvalidSessionIdException",
"SessionNotCreatedException",
"UnknownMethodException",
]
50 changes: 13 additions & 37 deletions py/selenium/common/exceptions.py
Expand Up @@ -28,33 +28,33 @@ class WebDriverException(Exception):
Base webdriver exception.
"""

def __init__(self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None) -> None:
def __init__(
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
) -> None:
self.msg = msg
self.screen = screen
self.stacktrace = stacktrace

def __str__(self) -> str:
exception_msg = "Message: %s\n" % self.msg
exception_msg = f"Message: {self.msg}\n"
if self.screen:
exception_msg += "Screenshot: available via screen\n"
if self.stacktrace:
stacktrace = "\n".join(self.stacktrace)
exception_msg += "Stacktrace:\n%s" % stacktrace
exception_msg += f"Stacktrace:\n{stacktrace}"
return exception_msg


class InvalidSwitchToTargetException(WebDriverException):
"""
Thrown when frame or window target to be switched doesn't exist.
"""
pass


class NoSuchFrameException(InvalidSwitchToTargetException):
"""
Thrown when frame target to be switched doesn't exist.
"""
pass


class NoSuchWindowException(InvalidSwitchToTargetException):
Expand All @@ -67,7 +67,6 @@ class NoSuchWindowException(InvalidSwitchToTargetException):
print driver.window_handles
"""
pass


class NoSuchElementException(WebDriverException):
Expand All @@ -80,7 +79,6 @@ class NoSuchElementException(WebDriverException):
(webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait()
for how to write a wait wrapper to wait for an element to appear.
"""
pass


class NoSuchAttributeException(WebDriverException):
Expand All @@ -91,15 +89,13 @@ class NoSuchAttributeException(WebDriverException):
testing against. Some browsers may have different property names for the same
property. (IE8's .innerText vs. Firefox .textContent)
"""
pass


class NoSuchShadowRootException(WebDriverException):
"""
Thrown when trying to access the shadow root of an element when it does not
have a shadow root attached.
"""
pass


class StaleElementReferenceException(WebDriverException):
Expand All @@ -118,7 +114,6 @@ class StaleElementReferenceException(WebDriverException):
node is rebuilt.
* Element may have been inside an iframe or another context which was refreshed.
"""
pass


class InvalidElementStateException(WebDriverException):
Expand All @@ -127,7 +122,6 @@ class InvalidElementStateException(WebDriverException):
This can be caused by attempting to clear an element that isn't both editable and resettable.
"""
pass


class UnexpectedAlertPresentException(WebDriverException):
Expand All @@ -138,7 +132,13 @@ class UnexpectedAlertPresentException(WebDriverException):
commands.
"""

def __init__(self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None, alert_text: Optional[str] = None) -> None:
def __init__(
self,
msg: Optional[str] = None,
screen: Optional[str] = None,
stacktrace: Optional[Sequence[str]] = None,
alert_text: Optional[str] = None,
) -> None:
super().__init__(msg, screen, stacktrace)
self.alert_text = alert_text

Expand All @@ -153,7 +153,6 @@ class NoAlertPresentException(WebDriverException):
This can be caused by calling an operation on the Alert() class when an alert is
not yet on the screen.
"""
pass


class ElementNotVisibleException(InvalidElementStateException):
Expand All @@ -164,15 +163,13 @@ class ElementNotVisibleException(InvalidElementStateException):
Most commonly encountered when trying to click or read text
of an element that is hidden from view.
"""
pass


class ElementNotInteractableException(InvalidElementStateException):
"""
Thrown when an element is present in the DOM but interactions
with that element will hit another element due to paint order
"""
pass


class ElementNotSelectableException(InvalidElementStateException):
Expand All @@ -181,50 +178,42 @@ class ElementNotSelectableException(InvalidElementStateException):
For example, selecting a 'script' element.
"""
pass


class InvalidCookieDomainException(WebDriverException):
"""
Thrown when attempting to add a cookie under a different domain
than the current URL.
"""
pass


class UnableToSetCookieException(WebDriverException):
"""
Thrown when a driver fails to set a cookie.
"""
pass


class RemoteDriverServerException(WebDriverException):
"""
"""
pass
"""Todo: Remove this class? it looks unused."""


class TimeoutException(WebDriverException):
"""
Thrown when a command does not complete in enough time.
"""
pass


class MoveTargetOutOfBoundsException(WebDriverException):
"""
Thrown when the target provided to the `ActionsChains` move()
method is invalid, i.e. out of document.
"""
pass


class UnexpectedTagNameException(WebDriverException):
"""
Thrown when a support class did not get an expected web element.
"""
pass


class InvalidSelectorException(WebDriverException):
Expand All @@ -235,93 +224,80 @@ class InvalidSelectorException(WebDriverException):
xpath expression) or the expression does not select WebElements
(e.g. "count(//input)").
"""
pass


class ImeNotAvailableException(WebDriverException):
"""
Thrown when IME support is not available. This exception is thrown for every IME-related
method call if IME support is not available on the machine.
"""
pass


class ImeActivationFailedException(WebDriverException):
"""
Thrown when activating an IME engine has failed.
"""
pass


class InvalidArgumentException(WebDriverException):
"""
The arguments passed to a command are either invalid or malformed.
"""
pass


class JavascriptException(WebDriverException):
"""
An error occurred while executing JavaScript supplied by the user.
"""
pass


class NoSuchCookieException(WebDriverException):
"""
No cookie matching the given path name was found amongst the associated cookies of the
current browsing context's active document.
"""
pass


class ScreenshotException(WebDriverException):
"""
A screen capture was made impossible.
"""
pass


class ElementClickInterceptedException(WebDriverException):
"""
The Element Click command could not be completed because the element receiving the events
is obscuring the element that was requested to be clicked.
"""
pass


class InsecureCertificateException(WebDriverException):
"""
Navigation caused the user agent to hit a certificate warning, which is usually the result
of an expired or invalid TLS certificate.
"""
pass


class InvalidCoordinatesException(WebDriverException):
"""
The coordinates provided to an interaction's operation are invalid.
"""
pass


class InvalidSessionIdException(WebDriverException):
"""
Occurs if the given session id is not in the list of active sessions, meaning the session
either does not exist or that it's not active.
"""
pass


class SessionNotCreatedException(WebDriverException):
"""
A new session could not be created.
"""
pass


class UnknownMethodException(WebDriverException):
"""
The requested command matched a known URL but did not match any methods for that URL.
"""
pass
4 changes: 2 additions & 2 deletions py/tox.ini
Expand Up @@ -41,6 +41,6 @@ deps =
flake8==5.0.4
flake8-typing-imports==1.13.0
commands =
isort selenium/ test/
black test/ -l 120
flake8 selenium/ test/ --min-python-version=3.7
isort selenium/ test/
black test/ selenium/common/ -l 120

0 comments on commit eaa7ecc

Please sign in to comment.