error_test.js 12 KB

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