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 | /** @deprecated */ |
50 | MODAL_DIALOG_OPENED: 26, |
51 | UNEXPECTED_ALERT_OPEN: 26, |
52 | NO_SUCH_ALERT: 27, |
53 | /** @deprecated */ |
54 | NO_MODAL_DIALOG_OPEN: 27, |
55 | SCRIPT_TIMEOUT: 28, |
56 | INVALID_ELEMENT_COORDINATES: 29, |
57 | IME_NOT_AVAILABLE: 30, |
58 | IME_ENGINE_ACTIVATION_FAILED: 31, |
59 | INVALID_SELECTOR_ERROR: 32, |
60 | SESSION_NOT_CREATED: 33, |
61 | MOVE_TARGET_OUT_OF_BOUNDS: 34, |
62 | SQL_DATABASE_ERROR: 35, |
63 | INVALID_XPATH_SELECTOR: 51, |
64 | INVALID_XPATH_SELECTOR_RETURN_TYPE: 52, |
65 | // The following error codes are derived straight from HTTP return codes. |
66 | METHOD_NOT_ALLOWED: 405 |
67 | }; |
68 | |
69 | |
70 | |
71 | /** |
72 | * Error extension that includes error status codes from the WebDriver wire |
73 | * protocol: |
74 | * http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes |
75 | * |
76 | * @param {!bot.ErrorCode} code The error's status code. |
77 | * @param {string=} opt_message Optional error message. |
78 | * @constructor |
79 | * @extends {Error} |
80 | */ |
81 | bot.Error = function(code, opt_message) { |
82 | |
83 | /** |
84 | * This error's status code. |
85 | * @type {!bot.ErrorCode} |
86 | */ |
87 | this.code = code; |
88 | |
89 | /** @type {string} */ |
90 | this.state = |
91 | bot.Error.CODE_TO_STATE_[code] || bot.Error.State.UNKNOWN_ERROR; |
92 | |
93 | /** @override */ |
94 | this.message = opt_message || ''; |
95 | |
96 | var name = this.state.replace(/((?:^|\s+)[a-z])/g, function(str) { |
97 | // IE<9 does not support String#trim(). Also, IE does not include 0xa0 |
98 | // (the non-breaking-space) in the \s character class, so we have to |
99 | // explicitly include it. |
100 | return str.toUpperCase().replace(/^[\s\xa0]+/g, ''); |
101 | }); |
102 | |
103 | var l = name.length - 'Error'.length; |
104 | if (l < 0 || name.indexOf('Error', l) != l) { |
105 | name += 'Error'; |
106 | } |
107 | |
108 | /** @override */ |
109 | this.name = name; |
110 | |
111 | // Generate a stacktrace for our custom error; ensure the error has our |
112 | // custom name and message so the stack prints correctly in all browsers. |
113 | var template = new Error(this.message); |
114 | template.name = this.name; |
115 | |
116 | /** @override */ |
117 | this.stack = template.stack || ''; |
118 | }; |
119 | goog.inherits(bot.Error, Error); |
120 | |
121 | |
122 | /** |
123 | * Status strings enumerated in the W3C WebDriver working draft. |
124 | * @enum {string} |
125 | * @see http://www.w3.org/TR/webdriver/#status-codes |
126 | */ |
127 | bot.Error.State = { |
128 | ELEMENT_NOT_SELECTABLE: 'element not selectable', |
129 | ELEMENT_NOT_VISIBLE: 'element not visible', |
130 | IME_ENGINE_ACTIVATION_FAILED: 'ime engine activation failed', |
131 | IME_NOT_AVAILABLE: 'ime not available', |
132 | INVALID_COOKIE_DOMAIN: 'invalid cookie domain', |
133 | INVALID_ELEMENT_COORDINATES: 'invalid element coordinates', |
134 | INVALID_ELEMENT_STATE: 'invalid element state', |
135 | INVALID_SELECTOR: 'invalid selector', |
136 | JAVASCRIPT_ERROR: 'javascript error', |
137 | MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds', |
138 | NO_SUCH_ALERT: 'no such alert', |
139 | NO_SUCH_DOM: 'no such dom', |
140 | NO_SUCH_ELEMENT: 'no such element', |
141 | NO_SUCH_FRAME: 'no such frame', |
142 | NO_SUCH_WINDOW: 'no such window', |
143 | SCRIPT_TIMEOUT: 'script timeout', |
144 | SESSION_NOT_CREATED: 'session not created', |
145 | STALE_ELEMENT_REFERENCE: 'stale element reference', |
146 | SUCCESS: 'success', |
147 | TIMEOUT: 'timeout', |
148 | UNABLE_TO_SET_COOKIE: 'unable to set cookie', |
149 | UNEXPECTED_ALERT_OPEN: 'unexpected alert open', |
150 | UNKNOWN_COMMAND: 'unknown command', |
151 | UNKNOWN_ERROR: 'unknown error', |
152 | UNSUPPORTED_OPERATION: 'unsupported operation' |
153 | }; |
154 | |
155 | |
156 | /** |
157 | * A map of error codes to state string. |
158 | * @private {!Object.<bot.ErrorCode, bot.Error.State>} |
159 | */ |
160 | bot.Error.CODE_TO_STATE_ = {}; |
161 | goog.scope(function() { |
162 | var map = bot.Error.CODE_TO_STATE_; |
163 | var code = bot.ErrorCode; |
164 | var state = bot.Error.State; |
165 | |
166 | map[code.ELEMENT_NOT_SELECTABLE] = state.ELEMENT_NOT_SELECTABLE; |
167 | map[code.ELEMENT_NOT_VISIBLE] = state.ELEMENT_NOT_VISIBLE; |
168 | map[code.IME_ENGINE_ACTIVATION_FAILED] = state.IME_ENGINE_ACTIVATION_FAILED; |
169 | map[code.IME_NOT_AVAILABLE] = state.IME_NOT_AVAILABLE; |
170 | map[code.INVALID_COOKIE_DOMAIN] = state.INVALID_COOKIE_DOMAIN; |
171 | map[code.INVALID_ELEMENT_COORDINATES] = state.INVALID_ELEMENT_COORDINATES; |
172 | map[code.INVALID_ELEMENT_STATE] = state.INVALID_ELEMENT_STATE; |
173 | map[code.INVALID_SELECTOR_ERROR] = state.INVALID_SELECTOR; |
174 | map[code.INVALID_XPATH_SELECTOR] = state.INVALID_SELECTOR; |
175 | map[code.INVALID_XPATH_SELECTOR_RETURN_TYPE] = state.INVALID_SELECTOR; |
176 | map[code.JAVASCRIPT_ERROR] = state.JAVASCRIPT_ERROR; |
177 | map[code.METHOD_NOT_ALLOWED] = state.UNSUPPORTED_OPERATION; |
178 | map[code.MOVE_TARGET_OUT_OF_BOUNDS] = state.MOVE_TARGET_OUT_OF_BOUNDS; |
179 | map[code.NO_MODAL_DIALOG_OPEN] = state.NO_SUCH_ALERT; |
180 | map[code.NO_SUCH_ALERT] = state.NO_SUCH_ALERT; |
181 | map[code.NO_SUCH_ELEMENT] = state.NO_SUCH_ELEMENT; |
182 | map[code.NO_SUCH_FRAME] = state.NO_SUCH_FRAME; |
183 | map[code.NO_SUCH_WINDOW] = state.NO_SUCH_WINDOW; |
184 | map[code.SCRIPT_TIMEOUT] = state.SCRIPT_TIMEOUT; |
185 | map[code.SESSION_NOT_CREATED] = state.SESSION_NOT_CREATED; |
186 | map[code.STALE_ELEMENT_REFERENCE] = state.STALE_ELEMENT_REFERENCE; |
187 | map[code.SUCCESS] = state.SUCCESS; |
188 | map[code.TIMEOUT] = state.TIMEOUT; |
189 | map[code.UNABLE_TO_SET_COOKIE] = state.UNABLE_TO_SET_COOKIE; |
190 | map[code.MODAL_DIALOG_OPENED] = state.UNEXPECTED_ALERT_OPEN; |
191 | map[code.UNEXPECTED_ALERT_OPEN] = state.UNEXPECTED_ALERT_OPEN |
192 | map[code.UNKNOWN_ERROR] = state.UNKNOWN_ERROR; |
193 | map[code.UNSUPPORTED_OPERATION] = state.UNKNOWN_COMMAND; |
194 | }); // goog.scope |
195 | |
196 | |
197 | /** |
198 | * Flag used for duck-typing when this code is embedded in a Firefox extension. |
199 | * This is required since an Error thrown in one component and then reported |
200 | * to another will fail instanceof checks in the second component. |
201 | * @type {boolean} |
202 | */ |
203 | bot.Error.prototype.isAutomationError = true; |
204 | |
205 | |
206 | if (goog.DEBUG) { |
207 | /** @return {string} The string representation of this error. */ |
208 | bot.Error.prototype.toString = function() { |
209 | return this.name + ': ' + this.message; |
210 | }; |
211 | } |