Skip to content

Commit

Permalink
[py] mapped all error codes in errorhandler.py to corresponding exc…
Browse files Browse the repository at this point in the history
…eption object. (#12190)

* [py] mapped each errorcode to corresponding exception

* [py] mapped each error codes to corresponding exception
  • Loading branch information
sandeepsuryaprasad committed Jun 12, 2023
1 parent 7cfd137 commit 2ebfcd9
Showing 1 changed file with 45 additions and 64 deletions.
109 changes: 45 additions & 64 deletions py/selenium/webdriver/remote/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@
from selenium.common.exceptions import WebDriverException


class ExceptionMapping:
"""Maps each errorcode in ErrorCode object to corresponding exception."""

NO_SUCH_ELEMENT = NoSuchElementException
NO_SUCH_FRAME = NoSuchFrameException
NO_SUCH_SHADOW_ROOT = NoSuchShadowRootException
STALE_ELEMENT_REFERENCE = StaleElementReferenceException
ELEMENT_NOT_VISIBLE = ElementNotVisibleException
INVALID_ELEMENT_STATE = InvalidElementStateException
UNKNOWN_ERROR = WebDriverException
ELEMENT_IS_NOT_SELECTABLE = ElementNotSelectableException
JAVASCRIPT_ERROR = JavascriptException
TIMEOUT = TimeoutException
NO_SUCH_WINDOW = NoSuchWindowException
INVALID_COOKIE_DOMAIN = InvalidCookieDomainException
UNABLE_TO_SET_COOKIE = UnableToSetCookieException
UNEXPECTED_ALERT_OPEN = UnexpectedAlertPresentException
NO_ALERT_OPEN = NoAlertPresentException
SCRIPT_TIMEOUT = TimeoutException
IME_NOT_AVAILABLE = ImeNotAvailableException
IME_ENGINE_ACTIVATION_FAILED = ImeActivationFailedException
INVALID_SELECTOR = InvalidSelectorException
SESSION_NOT_CREATED = SessionNotCreatedException
MOVE_TARGET_OUT_OF_BOUNDS = MoveTargetOutOfBoundsException
INVALID_XPATH_SELECTOR = InvalidSelectorException
INVALID_XPATH_SELECTOR_RETURN_TYPER = InvalidSelectorException
ELEMENT_NOT_INTERACTABLE = ElementNotInteractableException
INSECURE_CERTIFICATE = InsecureCertificateException
INVALID_ARGUMENT = InvalidArgumentException
INVALID_COORDINATES = InvalidCoordinatesException
INVALID_SESSION_ID = InvalidSessionIdException
NO_SUCH_COOKIE = NoSuchCookieException
UNABLE_TO_CAPTURE_SCREEN = ScreenshotException
ELEMENT_CLICK_INTERCEPTED = ElementClickInterceptedException
UNKNOWN_METHOD = UnknownMethodException


class ErrorCode:
"""Error codes defined in the WebDriver wire protocol."""

Expand Down Expand Up @@ -137,72 +174,16 @@ def check_response(self, response: Dict[str, Any]) -> None:
pass

exception_class: Type[WebDriverException]
if status in ErrorCode.NO_SUCH_ELEMENT:
exception_class = NoSuchElementException
elif status in ErrorCode.NO_SUCH_FRAME:
exception_class = NoSuchFrameException
elif status in ErrorCode.NO_SUCH_SHADOW_ROOT:
exception_class = NoSuchShadowRootException
elif status in ErrorCode.NO_SUCH_WINDOW:
exception_class = NoSuchWindowException
elif status in ErrorCode.STALE_ELEMENT_REFERENCE:
exception_class = StaleElementReferenceException
elif status in ErrorCode.ELEMENT_NOT_VISIBLE:
exception_class = ElementNotVisibleException
elif status in ErrorCode.INVALID_ELEMENT_STATE:
exception_class = InvalidElementStateException
elif (
status in ErrorCode.INVALID_SELECTOR
or status in ErrorCode.INVALID_XPATH_SELECTOR
or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER
):
exception_class = InvalidSelectorException
elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
exception_class = ElementNotSelectableException
elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE:
exception_class = ElementNotInteractableException
elif status in ErrorCode.INVALID_COOKIE_DOMAIN:
exception_class = InvalidCookieDomainException
elif status in ErrorCode.UNABLE_TO_SET_COOKIE:
exception_class = UnableToSetCookieException
elif status in ErrorCode.TIMEOUT:
exception_class = TimeoutException
elif status in ErrorCode.SCRIPT_TIMEOUT:
exception_class = TimeoutException
elif status in ErrorCode.UNKNOWN_ERROR:
exception_class = WebDriverException
elif status in ErrorCode.UNEXPECTED_ALERT_OPEN:
exception_class = UnexpectedAlertPresentException
elif status in ErrorCode.NO_ALERT_OPEN:
exception_class = NoAlertPresentException
elif status in ErrorCode.IME_NOT_AVAILABLE:
exception_class = ImeNotAvailableException
elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
exception_class = ImeActivationFailedException
elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
exception_class = MoveTargetOutOfBoundsException
elif status in ErrorCode.JAVASCRIPT_ERROR:
exception_class = JavascriptException
elif status in ErrorCode.SESSION_NOT_CREATED:
exception_class = SessionNotCreatedException
elif status in ErrorCode.INVALID_ARGUMENT:
exception_class = InvalidArgumentException
elif status in ErrorCode.NO_SUCH_COOKIE:
exception_class = NoSuchCookieException
elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN:
exception_class = ScreenshotException
elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED:
exception_class = ElementClickInterceptedException
elif status in ErrorCode.INSECURE_CERTIFICATE:
exception_class = InsecureCertificateException
elif status in ErrorCode.INVALID_COORDINATES:
exception_class = InvalidCoordinatesException
elif status in ErrorCode.INVALID_SESSION_ID:
exception_class = InvalidSessionIdException
elif status in ErrorCode.UNKNOWN_METHOD:
exception_class = UnknownMethodException
e = ErrorCode()
error_codes = [item for item in dir(e) if not item.startswith("__")]
for error_code in error_codes:
error_info = getattr(ErrorCode, error_code)
if isinstance(error_info, list) and status in error_info:
exception_class = getattr(ExceptionMapping, error_code, WebDriverException)
break
else:
exception_class = WebDriverException

if not value:
value = response["value"]
if isinstance(value, str):
Expand Down

0 comments on commit 2ebfcd9

Please sign in to comment.