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