Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/error_test.js
2885 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
'use strict'
19
20
describe('error', function () {
21
let assert = require('node:assert')
22
let error = require('selenium-webdriver/lib/error')
23
24
describe('encodeError', function () {
25
describe('defaults to an unknown error', function () {
26
it('for a generic error value', function () {
27
runTest('hi', 'unknown error', 'hi')
28
runTest(1, 'unknown error', '1')
29
runTest({}, 'unknown error', '[object Object]')
30
})
31
32
it('for a generic Error object', function () {
33
runTest(Error('oops'), 'unknown error', 'oops')
34
runTest(TypeError('bad value'), 'unknown error', 'bad value')
35
})
36
})
37
38
test(error.WebDriverError, 'unknown error')
39
test(error.ElementClickInterceptedError, 'element click intercepted')
40
test(error.ElementNotSelectableError, 'element not selectable')
41
test(error.InsecureCertificateError, 'insecure certificate')
42
test(error.InvalidArgumentError, 'invalid argument')
43
test(error.InvalidCookieDomainError, 'invalid cookie domain')
44
test(error.InvalidElementStateError, 'invalid element state')
45
test(error.InvalidSelectorError, 'invalid selector')
46
test(error.NoSuchSessionError, 'invalid session id')
47
test(error.JavascriptError, 'javascript error')
48
test(error.MoveTargetOutOfBoundsError, 'move target out of bounds')
49
test(error.NoSuchAlertError, 'no such alert')
50
test(error.NoSuchCookieError, 'no such cookie')
51
test(error.NoSuchElementError, 'no such element')
52
test(error.NoSuchFrameError, 'no such frame')
53
test(error.NoSuchWindowError, 'no such window')
54
test(error.ScriptTimeoutError, 'script timeout')
55
test(error.SessionNotCreatedError, 'session not created')
56
test(error.StaleElementReferenceError, 'stale element reference')
57
test(error.TimeoutError, 'timeout')
58
test(error.UnableToSetCookieError, 'unable to set cookie')
59
test(error.UnableToCaptureScreenError, 'unable to capture screen')
60
test(error.UnexpectedAlertOpenError, 'unexpected alert open')
61
test(error.UnknownCommandError, 'unknown command')
62
test(error.UnknownMethodError, 'unknown method')
63
test(error.UnsupportedOperationError, 'unsupported operation')
64
65
function test(ctor, code) {
66
it(`${ctor.name} => "${code}"`, () => {
67
runTest(new ctor('oops'), code, 'oops')
68
})
69
}
70
71
function runTest(err, code, message) {
72
let obj = error.encodeError(err)
73
assert.strictEqual(obj['error'], code)
74
assert.strictEqual(obj['message'], message)
75
}
76
})
77
78
describe('throwDecodedError', function () {
79
it('defaults to WebDriverError if type is unrecognized', function () {
80
assert.throws(
81
() => error.throwDecodedError({ error: 'foo', message: 'hi there' }),
82
(e) => {
83
assert.strictEqual(e.constructor, error.WebDriverError)
84
return true
85
},
86
)
87
})
88
89
it('throws generic error if encoded data is not valid', function () {
90
assert.throws(
91
() => error.throwDecodedError({ error: 123, message: 'abc123' }),
92
(e) => {
93
assert.strictEqual(e.constructor, error.WebDriverError)
94
return true
95
},
96
)
97
98
assert.throws(
99
() => error.throwDecodedError('null'),
100
(e) => {
101
assert.strictEqual(e.constructor, error.WebDriverError)
102
return true
103
},
104
)
105
106
assert.throws(
107
() => error.throwDecodedError(''),
108
(e) => {
109
assert.strictEqual(e.constructor, error.WebDriverError)
110
return true
111
},
112
)
113
})
114
115
test('unknown error', error.WebDriverError)
116
test('element click intercepted', error.ElementClickInterceptedError)
117
test('element not selectable', error.ElementNotSelectableError)
118
test('insecure certificate', error.InsecureCertificateError)
119
test('invalid argument', error.InvalidArgumentError)
120
test('invalid cookie domain', error.InvalidCookieDomainError)
121
test('invalid coordinates', error.InvalidCoordinatesError)
122
test('invalid element state', error.InvalidElementStateError)
123
test('invalid selector', error.InvalidSelectorError)
124
test('invalid session id', error.NoSuchSessionError)
125
test('javascript error', error.JavascriptError)
126
test('move target out of bounds', error.MoveTargetOutOfBoundsError)
127
test('no such alert', error.NoSuchAlertError)
128
test('no such cookie', error.NoSuchCookieError)
129
test('no such element', error.NoSuchElementError)
130
test('no such frame', error.NoSuchFrameError)
131
test('no such window', error.NoSuchWindowError)
132
test('script timeout', error.ScriptTimeoutError)
133
test('session not created', error.SessionNotCreatedError)
134
test('stale element reference', error.StaleElementReferenceError)
135
test('timeout', error.TimeoutError)
136
test('unable to set cookie', error.UnableToSetCookieError)
137
test('unable to capture screen', error.UnableToCaptureScreenError)
138
test('unexpected alert open', error.UnexpectedAlertOpenError)
139
test('unknown command', error.UnknownCommandError)
140
test('unknown method', error.UnknownMethodError)
141
test('unsupported operation', error.UnsupportedOperationError)
142
test('detached shadow root', error.DetachedShadowRootError)
143
144
it('leaves remoteStacktrace empty if not in encoding', function () {
145
assert.throws(
146
() =>
147
error.throwDecodedError({
148
error: 'session not created',
149
message: 'oops',
150
}),
151
(e) => {
152
assert.strictEqual(e.constructor, error.SessionNotCreatedError)
153
assert.strictEqual(e.message, 'oops')
154
assert.strictEqual(e.remoteStacktrace, '')
155
return true
156
},
157
)
158
})
159
160
function test(status, expectedType) {
161
it(`"${status}" => ${expectedType.name}`, function () {
162
assert.throws(
163
() =>
164
error.throwDecodedError({
165
error: status,
166
message: 'oops',
167
stacktrace: 'some-stacktrace',
168
}),
169
(e) => {
170
assert.strictEqual(e.constructor, expectedType)
171
assert.strictEqual(e.message, 'oops')
172
assert.strictEqual(e.remoteStacktrace, 'some-stacktrace')
173
return true
174
},
175
)
176
})
177
}
178
179
describe('remote stack trace decoding', function () {
180
test('stacktrace')
181
test('stackTrace')
182
183
function test(key) {
184
it(`encoded as "${key}"`, function () {
185
let data = { error: 'unknown command', message: 'oops' }
186
data[key] = 'some-stacktrace'
187
assert.throws(
188
() => error.throwDecodedError(data),
189
(e) => {
190
assert.strictEqual(e.remoteStacktrace, 'some-stacktrace')
191
return true
192
},
193
)
194
})
195
}
196
})
197
})
198
199
describe('checkLegacyResponse', function () {
200
it('does not throw for success', function () {
201
let resp = { status: error.ErrorCode.SUCCESS }
202
assert.strictEqual(resp, error.checkLegacyResponse(resp))
203
})
204
205
test('NO_SUCH_SESSION', error.NoSuchSessionError)
206
test('NO_SUCH_ELEMENT', error.NoSuchElementError)
207
test('NO_SUCH_FRAME', error.NoSuchFrameError)
208
test('UNKNOWN_COMMAND', error.UnsupportedOperationError)
209
test('UNSUPPORTED_OPERATION', error.UnsupportedOperationError)
210
test('STALE_ELEMENT_REFERENCE', error.StaleElementReferenceError)
211
test('INVALID_ELEMENT_STATE', error.InvalidElementStateError)
212
test('UNKNOWN_ERROR', error.WebDriverError)
213
test('ELEMENT_NOT_SELECTABLE', error.ElementNotSelectableError)
214
test('JAVASCRIPT_ERROR', error.JavascriptError)
215
test('XPATH_LOOKUP_ERROR', error.InvalidSelectorError)
216
test('TIMEOUT', error.TimeoutError)
217
test('NO_SUCH_WINDOW', error.NoSuchWindowError)
218
test('INVALID_COOKIE_DOMAIN', error.InvalidCookieDomainError)
219
test('UNABLE_TO_SET_COOKIE', error.UnableToSetCookieError)
220
test('UNEXPECTED_ALERT_OPEN', error.UnexpectedAlertOpenError)
221
test('NO_SUCH_ALERT', error.NoSuchAlertError)
222
test('SCRIPT_TIMEOUT', error.ScriptTimeoutError)
223
test('INVALID_ELEMENT_COORDINATES', error.InvalidCoordinatesError)
224
test('INVALID_SELECTOR_ERROR', error.InvalidSelectorError)
225
test('SESSION_NOT_CREATED', error.SessionNotCreatedError)
226
test('MOVE_TARGET_OUT_OF_BOUNDS', error.MoveTargetOutOfBoundsError)
227
test('INVALID_XPATH_SELECTOR', error.InvalidSelectorError)
228
test('INVALID_XPATH_SELECTOR_RETURN_TYPE', error.InvalidSelectorError)
229
test('ELEMENT_NOT_INTERACTABLE', error.ElementNotInteractableError)
230
test('INVALID_ARGUMENT', error.InvalidArgumentError)
231
test('UNABLE_TO_CAPTURE_SCREEN', error.UnableToCaptureScreenError)
232
test('ELEMENT_CLICK_INTERCEPTED', error.ElementClickInterceptedError)
233
test('METHOD_NOT_ALLOWED', error.UnsupportedOperationError)
234
test('DETACHED_SHADOW_ROOT', error.DetachedShadowRootError)
235
236
describe('UnexpectedAlertOpenError', function () {
237
it('includes alert text from the response object', function () {
238
let response = {
239
status: error.ErrorCode.UNEXPECTED_ALERT_OPEN,
240
value: {
241
message: 'hi',
242
alert: { text: 'alert text here' },
243
},
244
}
245
assert.throws(
246
() => error.checkLegacyResponse(response),
247
(e) => {
248
assert.strictEqual(error.UnexpectedAlertOpenError, e.constructor)
249
assert.strictEqual(e.message, 'hi')
250
assert.strictEqual(e.getAlertText(), 'alert text here')
251
return true
252
},
253
)
254
})
255
256
it('uses an empty string if alert text omitted', function () {
257
let response = {
258
status: error.ErrorCode.UNEXPECTED_ALERT_OPEN,
259
value: {
260
message: 'hi',
261
},
262
}
263
assert.throws(
264
() => error.checkLegacyResponse(response),
265
(e) => {
266
assert.strictEqual(error.UnexpectedAlertOpenError, e.constructor)
267
assert.strictEqual(e.message, 'hi')
268
assert.strictEqual(e.getAlertText(), '')
269
return true
270
},
271
)
272
})
273
})
274
275
function test(codeKey, expectedType) {
276
it(`${codeKey} => ${expectedType.name}`, function () {
277
let code = error.ErrorCode[codeKey]
278
let resp = { status: code, value: { message: 'hi' } }
279
assert.throws(
280
() => error.checkLegacyResponse(resp),
281
(e) => {
282
assert.strictEqual(expectedType, e.constructor)
283
assert.strictEqual(e.message, 'hi')
284
return true
285
},
286
)
287
})
288
}
289
})
290
})
291
292