| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'use strict' |
| |
| const { isObject } = require('./util') |
| |
| |
| |
| |
| |
| class WebDriverError extends Error { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| |
| |
| this.name = this.constructor.name |
| |
| |
| |
| |
| |
| |
| |
| this.remoteStacktrace = '' |
| } |
| } |
| |
| |
| |
| |
| class DetachedShadowRootError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| |
| class ElementClickInterceptedError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class ElementNotSelectableError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| class ElementNotInteractableError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class InsecureCertificateError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class InvalidArgumentError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class InvalidCookieDomainError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class InvalidCoordinatesError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| |
| class InvalidElementStateError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class InvalidSelectorError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class NoSuchSessionError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class JavascriptError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class MoveTargetOutOfBoundsError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class NoSuchAlertError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class NoSuchCookieError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class NoSuchElementError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class NoSuchShadowRootError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class NoSuchFrameError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class NoSuchWindowError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class ScriptTimeoutError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class SessionNotCreatedError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class StaleElementReferenceError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class TimeoutError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class UnableToSetCookieError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class UnableToCaptureScreenError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class UnexpectedAlertOpenError extends WebDriverError { |
| |
| |
| |
| |
| constructor(opt_error, opt_text) { |
| super(opt_error) |
| |
| |
| this.text_ = opt_text |
| } |
| |
| |
| |
| |
| |
| getAlertText() { |
| return this.text_ |
| } |
| } |
| |
| |
| |
| |
| class UnknownCommandError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| class UnknownMethodError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| class UnsupportedOperationError extends WebDriverError { |
| |
| constructor(opt_error) { |
| super(opt_error) |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const ErrorCode = { |
| SUCCESS: 0, |
| NO_SUCH_SESSION: 6, |
| NO_SUCH_ELEMENT: 7, |
| NO_SUCH_FRAME: 8, |
| UNKNOWN_COMMAND: 9, |
| UNSUPPORTED_OPERATION: 9, |
| STALE_ELEMENT_REFERENCE: 10, |
| ELEMENT_NOT_VISIBLE: 11, |
| INVALID_ELEMENT_STATE: 12, |
| UNKNOWN_ERROR: 13, |
| ELEMENT_NOT_SELECTABLE: 15, |
| JAVASCRIPT_ERROR: 17, |
| XPATH_LOOKUP_ERROR: 19, |
| TIMEOUT: 21, |
| NO_SUCH_WINDOW: 23, |
| INVALID_COOKIE_DOMAIN: 24, |
| UNABLE_TO_SET_COOKIE: 25, |
| UNEXPECTED_ALERT_OPEN: 26, |
| NO_SUCH_ALERT: 27, |
| SCRIPT_TIMEOUT: 28, |
| INVALID_ELEMENT_COORDINATES: 29, |
| IME_NOT_AVAILABLE: 30, |
| IME_ENGINE_ACTIVATION_FAILED: 31, |
| INVALID_SELECTOR_ERROR: 32, |
| SESSION_NOT_CREATED: 33, |
| MOVE_TARGET_OUT_OF_BOUNDS: 34, |
| SQL_DATABASE_ERROR: 35, |
| INVALID_XPATH_SELECTOR: 51, |
| INVALID_XPATH_SELECTOR_RETURN_TYPE: 52, |
| ELEMENT_NOT_INTERACTABLE: 60, |
| INVALID_ARGUMENT: 61, |
| NO_SUCH_COOKIE: 62, |
| UNABLE_TO_CAPTURE_SCREEN: 63, |
| ELEMENT_CLICK_INTERCEPTED: 64, |
| METHOD_NOT_ALLOWED: 405, |
| } |
| |
| const LEGACY_ERROR_CODE_TO_TYPE = new Map([ |
| [ErrorCode.NO_SUCH_SESSION, NoSuchSessionError], |
| [ErrorCode.NO_SUCH_ELEMENT, NoSuchElementError], |
| [ErrorCode.NO_SUCH_FRAME, NoSuchFrameError], |
| [ErrorCode.UNSUPPORTED_OPERATION, UnsupportedOperationError], |
| [ErrorCode.STALE_ELEMENT_REFERENCE, StaleElementReferenceError], |
| [ErrorCode.INVALID_ELEMENT_STATE, InvalidElementStateError], |
| [ErrorCode.UNKNOWN_ERROR, WebDriverError], |
| [ErrorCode.ELEMENT_NOT_SELECTABLE, ElementNotSelectableError], |
| [ErrorCode.JAVASCRIPT_ERROR, JavascriptError], |
| [ErrorCode.XPATH_LOOKUP_ERROR, InvalidSelectorError], |
| [ErrorCode.TIMEOUT, TimeoutError], |
| [ErrorCode.NO_SUCH_WINDOW, NoSuchWindowError], |
| [ErrorCode.INVALID_COOKIE_DOMAIN, InvalidCookieDomainError], |
| [ErrorCode.UNABLE_TO_SET_COOKIE, UnableToSetCookieError], |
| [ErrorCode.UNEXPECTED_ALERT_OPEN, UnexpectedAlertOpenError], |
| [ErrorCode.NO_SUCH_ALERT, NoSuchAlertError], |
| [ErrorCode.SCRIPT_TIMEOUT, ScriptTimeoutError], |
| [ErrorCode.INVALID_ELEMENT_COORDINATES, InvalidCoordinatesError], |
| [ErrorCode.INVALID_SELECTOR_ERROR, InvalidSelectorError], |
| [ErrorCode.SESSION_NOT_CREATED, SessionNotCreatedError], |
| [ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS, MoveTargetOutOfBoundsError], |
| [ErrorCode.INVALID_XPATH_SELECTOR, InvalidSelectorError], |
| [ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPE, InvalidSelectorError], |
| [ErrorCode.ELEMENT_NOT_INTERACTABLE, ElementNotInteractableError], |
| [ErrorCode.INVALID_ARGUMENT, InvalidArgumentError], |
| [ErrorCode.NO_SUCH_COOKIE, NoSuchCookieError], |
| [ErrorCode.UNABLE_TO_CAPTURE_SCREEN, UnableToCaptureScreenError], |
| [ErrorCode.ELEMENT_CLICK_INTERCEPTED, ElementClickInterceptedError], |
| [ErrorCode.METHOD_NOT_ALLOWED, UnsupportedOperationError], |
| ]) |
| |
| const ERROR_CODE_TO_TYPE = new Map([ |
| ['unknown error', WebDriverError], |
| ['detached shadow root', DetachedShadowRootError], |
| ['element click intercepted', ElementClickInterceptedError], |
| ['element not interactable', ElementNotInteractableError], |
| ['element not selectable', ElementNotSelectableError], |
| ['insecure certificate', InsecureCertificateError], |
| ['invalid argument', InvalidArgumentError], |
| ['invalid cookie domain', InvalidCookieDomainError], |
| ['invalid coordinates', InvalidCoordinatesError], |
| ['invalid element state', InvalidElementStateError], |
| ['invalid selector', InvalidSelectorError], |
| ['invalid session id', NoSuchSessionError], |
| ['javascript error', JavascriptError], |
| ['move target out of bounds', MoveTargetOutOfBoundsError], |
| ['no such alert', NoSuchAlertError], |
| ['no such cookie', NoSuchCookieError], |
| ['no such element', NoSuchElementError], |
| ['no such frame', NoSuchFrameError], |
| ['no such shadow root', NoSuchShadowRootError], |
| ['no such window', NoSuchWindowError], |
| ['script timeout', ScriptTimeoutError], |
| ['session not created', SessionNotCreatedError], |
| ['stale element reference', StaleElementReferenceError], |
| ['timeout', TimeoutError], |
| ['unable to set cookie', UnableToSetCookieError], |
| ['unable to capture screen', UnableToCaptureScreenError], |
| ['unexpected alert open', UnexpectedAlertOpenError], |
| ['unknown command', UnknownCommandError], |
| ['unknown method', UnknownMethodError], |
| ['unsupported operation', UnsupportedOperationError], |
| ]) |
| |
| const TYPE_TO_ERROR_CODE = new Map() |
| ERROR_CODE_TO_TYPE.forEach((value, key) => { |
| TYPE_TO_ERROR_CODE.set(value, key) |
| }) |
| |
| |
| |
| |
| |
| function encodeError(err) { |
| let type = WebDriverError |
| if (err instanceof WebDriverError && TYPE_TO_ERROR_CODE.has(err.constructor)) { |
| type = err.constructor |
| } |
| |
| let message = err instanceof Error ? err.message : err + '' |
| |
| let code = (TYPE_TO_ERROR_CODE.get(type)) |
| return { error: code, message: message } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function isErrorResponse(data) { |
| return isObject(data) && typeof data.error === 'string' |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function throwDecodedError(data) { |
| if (isErrorResponse(data)) { |
| let ctor = ERROR_CODE_TO_TYPE.get(data.error) || WebDriverError |
| let err = new ctor(data.message) |
| |
| if (typeof data.stacktrace === 'string') { |
| err.remoteStacktrace = data.stacktrace |
| } else if (typeof data.stackTrace === 'string') { |
| err.remoteStacktrace = data.stackTrace |
| } |
| throw err |
| } |
| throw new WebDriverError('Unknown error: ' + JSON.stringify(data)) |
| } |
| |
| |
| |
| |
| |
| |
| |
| function checkLegacyResponse(responseObj) { |
| |
| if (isObject(responseObj) && typeof responseObj.status === 'number' && responseObj.status !== 0) { |
| const { status, value } = responseObj |
| |
| let ctor = LEGACY_ERROR_CODE_TO_TYPE.get(status) || WebDriverError |
| |
| if (!value || typeof value !== 'object') { |
| throw new ctor(value + '') |
| } else { |
| let message = value['message'] + '' |
| if (ctor !== UnexpectedAlertOpenError) { |
| throw new ctor(message) |
| } |
| |
| let text = '' |
| if (value['alert'] && typeof value['alert']['text'] === 'string') { |
| text = value['alert']['text'] |
| } |
| throw new UnexpectedAlertOpenError(message, text) |
| } |
| } |
| return responseObj |
| } |
| |
| |
| |
| module.exports = { |
| ErrorCode, |
| |
| WebDriverError, |
| DetachedShadowRootError, |
| ElementClickInterceptedError, |
| ElementNotInteractableError, |
| ElementNotSelectableError, |
| InsecureCertificateError, |
| InvalidArgumentError, |
| InvalidCookieDomainError, |
| InvalidCoordinatesError, |
| InvalidElementStateError, |
| InvalidSelectorError, |
| JavascriptError, |
| MoveTargetOutOfBoundsError, |
| NoSuchAlertError, |
| NoSuchCookieError, |
| NoSuchElementError, |
| NoSuchFrameError, |
| NoSuchShadowRootError, |
| NoSuchSessionError, |
| NoSuchWindowError, |
| ScriptTimeoutError, |
| SessionNotCreatedError, |
| StaleElementReferenceError, |
| TimeoutError, |
| UnableToSetCookieError, |
| UnableToCaptureScreenError, |
| UnexpectedAlertOpenError, |
| UnknownCommandError, |
| UnknownMethodError, |
| UnsupportedOperationError, |
| checkLegacyResponse, |
| encodeError, |
| isErrorResponse, |
| throwDecodedError, |
| } |