Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/error.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
/**
19
* @fileoverview Utilities for working with errors as defined by WebDriver's
20
* wire protocol: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
21
*/
22
23
goog.provide('bot.Error');
24
goog.provide('bot.ErrorCode');
25
26
27
/**
28
* Error codes from the Selenium WebDriver protocol:
29
* https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#response-status-codes
30
*
31
* @enum {number}
32
* @suppress {lintChecks}
33
*/
34
bot.ErrorCode = {
35
SUCCESS: 0, // Included for completeness
36
37
NO_SUCH_ELEMENT: 7,
38
NO_SUCH_FRAME: 8,
39
UNKNOWN_COMMAND: 9,
40
UNSUPPORTED_OPERATION: 9, // Alias.
41
STALE_ELEMENT_REFERENCE: 10,
42
ELEMENT_NOT_VISIBLE: 11,
43
INVALID_ELEMENT_STATE: 12,
44
UNKNOWN_ERROR: 13,
45
ELEMENT_NOT_SELECTABLE: 15,
46
JAVASCRIPT_ERROR: 17,
47
XPATH_LOOKUP_ERROR: 19,
48
TIMEOUT: 21,
49
NO_SUCH_WINDOW: 23,
50
INVALID_COOKIE_DOMAIN: 24,
51
UNABLE_TO_SET_COOKIE: 25,
52
UNEXPECTED_ALERT_OPEN: 26,
53
NO_SUCH_ALERT: 27,
54
SCRIPT_TIMEOUT: 28,
55
INVALID_ELEMENT_COORDINATES: 29,
56
IME_NOT_AVAILABLE: 30,
57
IME_ENGINE_ACTIVATION_FAILED: 31,
58
INVALID_SELECTOR_ERROR: 32,
59
SESSION_NOT_CREATED: 33,
60
MOVE_TARGET_OUT_OF_BOUNDS: 34,
61
SQL_DATABASE_ERROR: 35,
62
INVALID_XPATH_SELECTOR: 51,
63
INVALID_XPATH_SELECTOR_RETURN_TYPE: 52,
64
INVALID_ARGUMENT: 61,
65
// The following error codes are derived straight from HTTP return codes.
66
METHOD_NOT_ALLOWED: 405
67
};
68
69
70
/**
71
* Represents an error returned from a WebDriver command request.
72
*
73
* @param {!bot.ErrorCode} code The error's status code.
74
* @param {string=} opt_message Optional error message.
75
* @constructor
76
* @extends {Error}
77
*/
78
bot.Error = function (code, opt_message) {
79
80
/**
81
* This error's status code.
82
* @type {!bot.ErrorCode}
83
*/
84
this.code = code;
85
86
/** @type {string} */
87
this.state =
88
bot.Error.CODE_TO_STATE_[code] || bot.Error.State.UNKNOWN_ERROR;
89
90
/** @override */
91
this.message = opt_message || '';
92
93
var name = this.state.replace(/((?:^|\s+)[a-z])/g, function (str) {
94
// IE<9 does not support String#trim(). Also, IE does not include 0xa0
95
// (the non-breaking-space) in the \s character class, so we have to
96
// explicitly include it.
97
return str.toUpperCase().replace(/^[\s\xa0]+/g, '');
98
});
99
100
var l = name.length - 'Error'.length;
101
if (l < 0 || name.indexOf('Error', l) != l) {
102
name += 'Error';
103
}
104
105
/** @override */
106
this.name = name;
107
108
// Generate a stacktrace for our custom error; ensure the error has our
109
// custom name and message so the stack prints correctly in all browsers.
110
var template = new Error(this.message);
111
template.name = this.name;
112
113
/** @override */
114
this.stack = template.stack || '';
115
};
116
goog.inherits(bot.Error, Error);
117
118
119
/**
120
* Status strings enumerated in the W3C WebDriver protocol.
121
* @enum {string}
122
* @see https://w3c.github.io/webdriver/webdriver-spec.html#handling-errors
123
*/
124
bot.Error.State = {
125
ELEMENT_NOT_SELECTABLE: 'element not selectable',
126
ELEMENT_NOT_VISIBLE: 'element not visible',
127
INVALID_ARGUMENT: 'invalid argument',
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
INVALID_SESSION_ID: 'invalid session id',
133
JAVASCRIPT_ERROR: 'javascript error',
134
MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds',
135
NO_SUCH_ALERT: 'no such alert',
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
TIMEOUT: 'timeout',
143
UNABLE_TO_SET_COOKIE: 'unable to set cookie',
144
UNEXPECTED_ALERT_OPEN: 'unexpected alert open',
145
UNKNOWN_COMMAND: 'unknown command',
146
UNKNOWN_ERROR: 'unknown error',
147
UNKNOWN_METHOD: 'unknown method',
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.UNKNOWN_ERROR;
165
map[code.IME_NOT_AVAILABLE] = state.UNKNOWN_ERROR;
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_SUCH_ALERT] = 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.TIMEOUT] = state.TIMEOUT;
183
map[code.UNABLE_TO_SET_COOKIE] = state.UNABLE_TO_SET_COOKIE;
184
map[code.UNEXPECTED_ALERT_OPEN] = state.UNEXPECTED_ALERT_OPEN;
185
map[code.UNKNOWN_ERROR] = state.UNKNOWN_ERROR;
186
map[code.UNSUPPORTED_OPERATION] = state.UNKNOWN_COMMAND;
187
}); // goog.scope
188
189
190
/**
191
* Flag used for duck-typing when this code is embedded in a Firefox extension.
192
* This is required since an Error thrown in one component and then reported
193
* to another will fail instanceof checks in the second component.
194
* @type {boolean}
195
*/
196
bot.Error.prototype.isAutomationError = true;
197
198
199
if (goog.DEBUG) {
200
/**
201
* @override
202
* @return {string} The string representation of this error.
203
*/
204
bot.Error.prototype.toString = function () {
205
return this.name + ': ' + this.message;
206
};
207
}
208
209