Path: blob/trunk/javascript/selenium-webdriver/test/lib/error_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819describe('error', function () {20let assert = require('node:assert')21let error = require('selenium-webdriver/lib/error')2223describe('encodeError', function () {24describe('defaults to an unknown error', function () {25it('for a generic error value', function () {26runTest('hi', 'unknown error', 'hi')27runTest(1, 'unknown error', '1')28runTest({}, 'unknown error', '[object Object]')29})3031it('for a generic Error object', function () {32runTest(Error('oops'), 'unknown error', 'oops')33runTest(TypeError('bad value'), 'unknown error', 'bad value')34})35})3637test(error.WebDriverError, 'unknown error')38test(error.ElementClickInterceptedError, 'element click intercepted')39test(error.ElementNotSelectableError, 'element not selectable')40test(error.InsecureCertificateError, 'insecure certificate')41test(error.InvalidArgumentError, 'invalid argument')42test(error.InvalidCookieDomainError, 'invalid cookie domain')43test(error.InvalidElementStateError, 'invalid element state')44test(error.InvalidSelectorError, 'invalid selector')45test(error.NoSuchSessionError, 'invalid session id')46test(error.JavascriptError, 'javascript error')47test(error.MoveTargetOutOfBoundsError, 'move target out of bounds')48test(error.NoSuchAlertError, 'no such alert')49test(error.NoSuchCookieError, 'no such cookie')50test(error.NoSuchElementError, 'no such element')51test(error.NoSuchFrameError, 'no such frame')52test(error.NoSuchWindowError, 'no such window')53test(error.ScriptTimeoutError, 'script timeout')54test(error.SessionNotCreatedError, 'session not created')55test(error.StaleElementReferenceError, 'stale element reference')56test(error.TimeoutError, 'timeout')57test(error.UnableToSetCookieError, 'unable to set cookie')58test(error.UnableToCaptureScreenError, 'unable to capture screen')59test(error.UnexpectedAlertOpenError, 'unexpected alert open')60test(error.UnknownCommandError, 'unknown command')61test(error.UnknownMethodError, 'unknown method')62test(error.UnsupportedOperationError, 'unsupported operation')6364function test(ctor, code) {65it(`${ctor.name} => "${code}"`, () => {66runTest(new ctor('oops'), code, 'oops')67})68}6970function runTest(err, code, message) {71let obj = error.encodeError(err)72assert.strictEqual(obj['error'], code)73assert.strictEqual(obj['message'], message)74}75})7677describe('throwDecodedError', function () {78it('defaults to WebDriverError if type is unrecognized', function () {79assert.throws(80() => error.throwDecodedError({ error: 'foo', message: 'hi there' }),81(e) => {82assert.strictEqual(e.constructor, error.WebDriverError)83return true84},85)86})8788it('throws generic error if encoded data is not valid', function () {89assert.throws(90() => error.throwDecodedError({ error: 123, message: 'abc123' }),91(e) => {92assert.strictEqual(e.constructor, error.WebDriverError)93return true94},95)9697assert.throws(98() => error.throwDecodedError('null'),99(e) => {100assert.strictEqual(e.constructor, error.WebDriverError)101return true102},103)104105assert.throws(106() => error.throwDecodedError(''),107(e) => {108assert.strictEqual(e.constructor, error.WebDriverError)109return true110},111)112})113114test('unknown error', error.WebDriverError)115test('element click intercepted', error.ElementClickInterceptedError)116test('element not selectable', error.ElementNotSelectableError)117test('insecure certificate', error.InsecureCertificateError)118test('invalid argument', error.InvalidArgumentError)119test('invalid cookie domain', error.InvalidCookieDomainError)120test('invalid coordinates', error.InvalidCoordinatesError)121test('invalid element state', error.InvalidElementStateError)122test('invalid selector', error.InvalidSelectorError)123test('invalid session id', error.NoSuchSessionError)124test('javascript error', error.JavascriptError)125test('move target out of bounds', error.MoveTargetOutOfBoundsError)126test('no such alert', error.NoSuchAlertError)127test('no such cookie', error.NoSuchCookieError)128test('no such element', error.NoSuchElementError)129test('no such frame', error.NoSuchFrameError)130test('no such window', error.NoSuchWindowError)131test('script timeout', error.ScriptTimeoutError)132test('session not created', error.SessionNotCreatedError)133test('stale element reference', error.StaleElementReferenceError)134test('timeout', error.TimeoutError)135test('unable to set cookie', error.UnableToSetCookieError)136test('unable to capture screen', error.UnableToCaptureScreenError)137test('unexpected alert open', error.UnexpectedAlertOpenError)138test('unknown command', error.UnknownCommandError)139test('unknown method', error.UnknownMethodError)140test('unsupported operation', error.UnsupportedOperationError)141test('detached shadow root', error.DetachedShadowRootError)142143it('leaves remoteStacktrace empty if not in encoding', function () {144assert.throws(145() =>146error.throwDecodedError({147error: 'session not created',148message: 'oops',149}),150(e) => {151assert.strictEqual(e.constructor, error.SessionNotCreatedError)152assert.strictEqual(e.message, 'oops')153assert.strictEqual(e.remoteStacktrace, '')154return true155},156)157})158159function test(status, expectedType) {160it(`"${status}" => ${expectedType.name}`, function () {161assert.throws(162() =>163error.throwDecodedError({164error: status,165message: 'oops',166stacktrace: 'some-stacktrace',167}),168(e) => {169assert.strictEqual(e.constructor, expectedType)170assert.strictEqual(e.message, 'oops')171assert.strictEqual(e.remoteStacktrace, 'some-stacktrace')172return true173},174)175})176}177178describe('remote stack trace decoding', function () {179test('stacktrace')180test('stackTrace')181182function test(key) {183it(`encoded as "${key}"`, function () {184let data = { error: 'unknown command', message: 'oops' }185data[key] = 'some-stacktrace'186assert.throws(187() => error.throwDecodedError(data),188(e) => {189assert.strictEqual(e.remoteStacktrace, 'some-stacktrace')190return true191},192)193})194}195})196})197198describe('checkLegacyResponse', function () {199it('does not throw for success', function () {200let resp = { status: error.ErrorCode.SUCCESS }201assert.strictEqual(resp, error.checkLegacyResponse(resp))202})203204test('NO_SUCH_SESSION', error.NoSuchSessionError)205test('NO_SUCH_ELEMENT', error.NoSuchElementError)206test('NO_SUCH_FRAME', error.NoSuchFrameError)207test('UNKNOWN_COMMAND', error.UnsupportedOperationError)208test('UNSUPPORTED_OPERATION', error.UnsupportedOperationError)209test('STALE_ELEMENT_REFERENCE', error.StaleElementReferenceError)210test('INVALID_ELEMENT_STATE', error.InvalidElementStateError)211test('UNKNOWN_ERROR', error.WebDriverError)212test('ELEMENT_NOT_SELECTABLE', error.ElementNotSelectableError)213test('JAVASCRIPT_ERROR', error.JavascriptError)214test('XPATH_LOOKUP_ERROR', error.InvalidSelectorError)215test('TIMEOUT', error.TimeoutError)216test('NO_SUCH_WINDOW', error.NoSuchWindowError)217test('INVALID_COOKIE_DOMAIN', error.InvalidCookieDomainError)218test('UNABLE_TO_SET_COOKIE', error.UnableToSetCookieError)219test('UNEXPECTED_ALERT_OPEN', error.UnexpectedAlertOpenError)220test('NO_SUCH_ALERT', error.NoSuchAlertError)221test('SCRIPT_TIMEOUT', error.ScriptTimeoutError)222test('INVALID_ELEMENT_COORDINATES', error.InvalidCoordinatesError)223test('INVALID_SELECTOR_ERROR', error.InvalidSelectorError)224test('SESSION_NOT_CREATED', error.SessionNotCreatedError)225test('MOVE_TARGET_OUT_OF_BOUNDS', error.MoveTargetOutOfBoundsError)226test('INVALID_XPATH_SELECTOR', error.InvalidSelectorError)227test('INVALID_XPATH_SELECTOR_RETURN_TYPE', error.InvalidSelectorError)228test('ELEMENT_NOT_INTERACTABLE', error.ElementNotInteractableError)229test('INVALID_ARGUMENT', error.InvalidArgumentError)230test('UNABLE_TO_CAPTURE_SCREEN', error.UnableToCaptureScreenError)231test('ELEMENT_CLICK_INTERCEPTED', error.ElementClickInterceptedError)232test('METHOD_NOT_ALLOWED', error.UnsupportedOperationError)233test('DETACHED_SHADOW_ROOT', error.DetachedShadowRootError)234235describe('UnexpectedAlertOpenError', function () {236it('includes alert text from the response object', function () {237let response = {238status: error.ErrorCode.UNEXPECTED_ALERT_OPEN,239value: {240message: 'hi',241alert: { text: 'alert text here' },242},243}244assert.throws(245() => error.checkLegacyResponse(response),246(e) => {247assert.strictEqual(error.UnexpectedAlertOpenError, e.constructor)248assert.strictEqual(e.message, 'hi')249assert.strictEqual(e.getAlertText(), 'alert text here')250return true251},252)253})254255it('uses an empty string if alert text omitted', function () {256let response = {257status: error.ErrorCode.UNEXPECTED_ALERT_OPEN,258value: {259message: 'hi',260},261}262assert.throws(263() => error.checkLegacyResponse(response),264(e) => {265assert.strictEqual(error.UnexpectedAlertOpenError, e.constructor)266assert.strictEqual(e.message, 'hi')267assert.strictEqual(e.getAlertText(), '')268return true269},270)271})272})273274function test(codeKey, expectedType) {275it(`${codeKey} => ${expectedType.name}`, function () {276let code = error.ErrorCode[codeKey]277let resp = { status: code, value: { message: 'hi' } }278assert.throws(279() => error.checkLegacyResponse(resp),280(e) => {281assert.strictEqual(expectedType, e.constructor)282assert.strictEqual(e.message, 'hi')283return true284},285)286})287}288})289})290291292