| 1 | // Copyright 2010 WebDriver committers |
| 2 | // Copyright 2010 Google Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | /** |
| 17 | * @fileoverview Utilities for working with errors as defined by WebDriver's |
| 18 | * wire protocol: http://code.google.com/p/selenium/wiki/JsonWireProtocol. |
| 19 | */ |
| 20 | |
| 21 | goog.provide('bot.Error'); |
| 22 | goog.provide('bot.ErrorCode'); |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Error codes from the WebDriver wire protocol: |
| 27 | * http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes |
| 28 | * |
| 29 | * @enum {number} |
| 30 | */ |
| 31 | bot.ErrorCode = { |
| 32 | SUCCESS: 0, // Included for completeness |
| 33 | |
| 34 | NO_SUCH_ELEMENT: 7, |
| 35 | NO_SUCH_FRAME: 8, |
| 36 | UNKNOWN_COMMAND: 9, |
| 37 | UNSUPPORTED_OPERATION: 9, // Alias. |
| 38 | STALE_ELEMENT_REFERENCE: 10, |
| 39 | ELEMENT_NOT_VISIBLE: 11, |
| 40 | INVALID_ELEMENT_STATE: 12, |
| 41 | UNKNOWN_ERROR: 13, |
| 42 | ELEMENT_NOT_SELECTABLE: 15, |
| 43 | JAVASCRIPT_ERROR: 17, |
| 44 | XPATH_LOOKUP_ERROR: 19, |
| 45 | TIMEOUT: 21, |
| 46 | NO_SUCH_WINDOW: 23, |
| 47 | INVALID_COOKIE_DOMAIN: 24, |
| 48 | UNABLE_TO_SET_COOKIE: 25, |
| 49 | MODAL_DIALOG_OPENED: 26, |
| 50 | NO_MODAL_DIALOG_OPEN: 27, |
| 51 | SCRIPT_TIMEOUT: 28, |
| 52 | INVALID_ELEMENT_COORDINATES: 29, |
| 53 | IME_NOT_AVAILABLE: 30, |
| 54 | IME_ENGINE_ACTIVATION_FAILED: 31, |
| 55 | INVALID_SELECTOR_ERROR: 32, |
| 56 | SESSION_NOT_CREATED: 33, |
| 57 | MOVE_TARGET_OUT_OF_BOUNDS: 34, |
| 58 | SQL_DATABASE_ERROR: 35, |
| 59 | INVALID_XPATH_SELECTOR: 51, |
| 60 | INVALID_XPATH_SELECTOR_RETURN_TYPE: 52, |
| 61 | // The following error codes are derived straight from HTTP return codes. |
| 62 | METHOD_NOT_ALLOWED: 405 |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Error extension that includes error status codes from the WebDriver wire |
| 69 | * protocol: |
| 70 | * http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes |
| 71 | * |
| 72 | * @param {!bot.ErrorCode} code The error's status code. |
| 73 | * @param {string=} opt_message Optional error message. |
| 74 | * @constructor |
| 75 | * @extends {Error} |
| 76 | */ |
| 77 | bot.Error = function(code, opt_message) { |
| 78 | |
| 79 | /** |
| 80 | * This error's status code. |
| 81 | * @type {!bot.ErrorCode} |
| 82 | */ |
| 83 | this.code = code; |
| 84 | |
| 85 | /** @type {string} */ |
| 86 | this.state = |
| 87 | bot.Error.CODE_TO_STATE_[code] || bot.Error.State.UNKNOWN_ERROR; |
| 88 | |
| 89 | /** @override */ |
| 90 | this.message = opt_message || ''; |
| 91 | |
| 92 | var name = this.state.replace(/((?:^|\s+)[a-z])/g, function(str) { |
| 93 | // IE<9 does not support String#trim(). Also, IE does not include 0xa0 |
| 94 | // (the non-breaking-space) in the \s character class, so we have to |
| 95 | // explicitly include it. |
| 96 | return str.toUpperCase().replace(/^[\s\xa0]+/g, ''); |
| 97 | }); |
| 98 | |
| 99 | var l = name.length - 'Error'.length; |
| 100 | if (l < 0 || name.indexOf('Error', l) != l) { |
| 101 | name += 'Error'; |
| 102 | } |
| 103 | |
| 104 | /** @override */ |
| 105 | this.name = name; |
| 106 | |
| 107 | // Generate a stacktrace for our custom error; ensure the error has our |
| 108 | // custom name and message so the stack prints correctly in all browsers. |
| 109 | var template = new Error(this.message); |
| 110 | template.name = this.name; |
| 111 | |
| 112 | /** @override */ |
| 113 | this.stack = template.stack || ''; |
| 114 | }; |
| 115 | goog.inherits(bot.Error, Error); |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Status strings enumerated in the W3C WebDriver working draft. |
| 120 | * @enum {string} |
| 121 | * @see http://www.w3.org/TR/webdriver/#status-codes |
| 122 | */ |
| 123 | bot.Error.State = { |
| 124 | ELEMENT_NOT_SELECTABLE: 'element not selectable', |
| 125 | ELEMENT_NOT_VISIBLE: 'element not visible', |
| 126 | IME_ENGINE_ACTIVATION_FAILED: 'ime engine activation failed', |
| 127 | IME_NOT_AVAILABLE: 'ime not available', |
| 128 | INVALID_COOKIE_DOMAIN: 'invalid cookie domain', |
| 129 | INVALID_ELEMENT_COORDINATES: 'invalid element coordinates', |
| 130 | INVALID_ELEMENT_STATE: 'invalid element state', |
| 131 | INVALID_SELECTOR: 'invalid selector', |
| 132 | JAVASCRIPT_ERROR: 'javascript error', |
| 133 | MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds', |
| 134 | NO_SUCH_ALERT: 'no such alert', |
| 135 | NO_SUCH_DOM: 'no such dom', |
| 136 | NO_SUCH_ELEMENT: 'no such element', |
| 137 | NO_SUCH_FRAME: 'no such frame', |
| 138 | NO_SUCH_WINDOW: 'no such window', |
| 139 | SCRIPT_TIMEOUT: 'script timeout', |
| 140 | SESSION_NOT_CREATED: 'session not created', |
| 141 | STALE_ELEMENT_REFERENCE: 'stale element reference', |
| 142 | SUCCESS: 'success', |
| 143 | TIMEOUT: 'timeout', |
| 144 | UNABLE_TO_SET_COOKIE: 'unable to set cookie', |
| 145 | UNEXPECTED_ALERT_OPEN: 'unexpected alert open', |
| 146 | UNKNOWN_COMMAND: 'unknown command', |
| 147 | UNKNOWN_ERROR: 'unknown error', |
| 148 | UNSUPPORTED_OPERATION: 'unsupported operation' |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * A map of error codes to state string. |
| 154 | * @private {!Object.<bot.ErrorCode, bot.Error.State>} |
| 155 | */ |
| 156 | bot.Error.CODE_TO_STATE_ = {}; |
| 157 | goog.scope(function() { |
| 158 | var map = bot.Error.CODE_TO_STATE_; |
| 159 | var code = bot.ErrorCode; |
| 160 | var state = bot.Error.State; |
| 161 | |
| 162 | map[code.ELEMENT_NOT_SELECTABLE] = state.ELEMENT_NOT_SELECTABLE; |
| 163 | map[code.ELEMENT_NOT_VISIBLE] = state.ELEMENT_NOT_VISIBLE; |
| 164 | map[code.IME_ENGINE_ACTIVATION_FAILED] = state.IME_ENGINE_ACTIVATION_FAILED; |
| 165 | map[code.IME_NOT_AVAILABLE] = state.IME_NOT_AVAILABLE; |
| 166 | map[code.INVALID_COOKIE_DOMAIN] = state.INVALID_COOKIE_DOMAIN; |
| 167 | map[code.INVALID_ELEMENT_COORDINATES] = state.INVALID_ELEMENT_COORDINATES; |
| 168 | map[code.INVALID_ELEMENT_STATE] = state.INVALID_ELEMENT_STATE; |
| 169 | map[code.INVALID_SELECTOR_ERROR] = state.INVALID_SELECTOR; |
| 170 | map[code.INVALID_XPATH_SELECTOR] = state.INVALID_SELECTOR; |
| 171 | map[code.INVALID_XPATH_SELECTOR_RETURN_TYPE] = state.INVALID_SELECTOR; |
| 172 | map[code.JAVASCRIPT_ERROR] = state.JAVASCRIPT_ERROR; |
| 173 | map[code.METHOD_NOT_ALLOWED] = state.UNSUPPORTED_OPERATION; |
| 174 | map[code.MOVE_TARGET_OUT_OF_BOUNDS] = state.MOVE_TARGET_OUT_OF_BOUNDS; |
| 175 | map[code.NO_MODAL_DIALOG_OPEN] = state.NO_SUCH_ALERT; |
| 176 | map[code.NO_SUCH_ELEMENT] = state.NO_SUCH_ELEMENT; |
| 177 | map[code.NO_SUCH_FRAME] = state.NO_SUCH_FRAME; |
| 178 | map[code.NO_SUCH_WINDOW] = state.NO_SUCH_WINDOW; |
| 179 | map[code.SCRIPT_TIMEOUT] = state.SCRIPT_TIMEOUT; |
| 180 | map[code.SESSION_NOT_CREATED] = state.SESSION_NOT_CREATED; |
| 181 | map[code.STALE_ELEMENT_REFERENCE] = state.STALE_ELEMENT_REFERENCE; |
| 182 | map[code.SUCCESS] = state.SUCCESS; |
| 183 | map[code.TIMEOUT] = state.TIMEOUT; |
| 184 | map[code.UNABLE_TO_SET_COOKIE] = state.UNABLE_TO_SET_COOKIE; |
| 185 | map[code.MODAL_DIALOG_OPENED] = state.UNEXPECTED_ALERT_OPEN; |
| 186 | map[code.UNKNOWN_ERROR] = state.UNKNOWN_ERROR; |
| 187 | map[code.UNSUPPORTED_OPERATION] = state.UNKNOWN_COMMAND; |
| 188 | }); // goog.scope |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Flag used for duck-typing when this code is embedded in a Firefox extension. |
| 193 | * This is required since an Error thrown in one component and then reported |
| 194 | * to another will fail instanceof checks in the second component. |
| 195 | * @type {boolean} |
| 196 | */ |
| 197 | bot.Error.prototype.isAutomationError = true; |
| 198 | |
| 199 | |
| 200 | if (goog.DEBUG) { |
| 201 | /** @return {string} The string representation of this error. */ |
| 202 | bot.Error.prototype.toString = function() { |
| 203 | return this.name + ': ' + this.message; |
| 204 | }; |
| 205 | } |