PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/protractor/node_modules/selenium-webdriver/lib/atoms/error.js

https://github.com/hezzze/ng_mobile_test
JavaScript | 205 lines | 116 code | 25 blank | 64 comment | 7 complexity | 719a1c8434117a88487bd8a5123f518b MD5 | raw file
Possible License(s): MIT, 0BSD, Apache-2.0
  1. // Copyright 2010 WebDriver committers
  2. // Copyright 2010 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. /**
  16. * @fileoverview Utilities for working with errors as defined by WebDriver's
  17. * wire protocol: http://code.google.com/p/selenium/wiki/JsonWireProtocol.
  18. */
  19. goog.provide('bot.Error');
  20. goog.provide('bot.ErrorCode');
  21. /**
  22. * Error codes from the WebDriver wire protocol:
  23. * http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes
  24. *
  25. * @enum {number}
  26. */
  27. bot.ErrorCode = {
  28. SUCCESS: 0, // Included for completeness
  29. NO_SUCH_ELEMENT: 7,
  30. NO_SUCH_FRAME: 8,
  31. UNKNOWN_COMMAND: 9,
  32. UNSUPPORTED_OPERATION: 9, // Alias.
  33. STALE_ELEMENT_REFERENCE: 10,
  34. ELEMENT_NOT_VISIBLE: 11,
  35. INVALID_ELEMENT_STATE: 12,
  36. UNKNOWN_ERROR: 13,
  37. ELEMENT_NOT_SELECTABLE: 15,
  38. JAVASCRIPT_ERROR: 17,
  39. XPATH_LOOKUP_ERROR: 19,
  40. TIMEOUT: 21,
  41. NO_SUCH_WINDOW: 23,
  42. INVALID_COOKIE_DOMAIN: 24,
  43. UNABLE_TO_SET_COOKIE: 25,
  44. MODAL_DIALOG_OPENED: 26,
  45. NO_MODAL_DIALOG_OPEN: 27,
  46. SCRIPT_TIMEOUT: 28,
  47. INVALID_ELEMENT_COORDINATES: 29,
  48. IME_NOT_AVAILABLE: 30,
  49. IME_ENGINE_ACTIVATION_FAILED: 31,
  50. INVALID_SELECTOR_ERROR: 32,
  51. SESSION_NOT_CREATED: 33,
  52. MOVE_TARGET_OUT_OF_BOUNDS: 34,
  53. SQL_DATABASE_ERROR: 35,
  54. INVALID_XPATH_SELECTOR: 51,
  55. INVALID_XPATH_SELECTOR_RETURN_TYPE: 52,
  56. // The following error codes are derived straight from HTTP return codes.
  57. METHOD_NOT_ALLOWED: 405
  58. };
  59. /**
  60. * Error extension that includes error status codes from the WebDriver wire
  61. * protocol:
  62. * http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes
  63. *
  64. * @param {!bot.ErrorCode} code The error's status code.
  65. * @param {string=} opt_message Optional error message.
  66. * @constructor
  67. * @extends {Error}
  68. */
  69. bot.Error = function(code, opt_message) {
  70. /**
  71. * This error's status code.
  72. * @type {!bot.ErrorCode}
  73. */
  74. this.code = code;
  75. /** @type {string} */
  76. this.state =
  77. bot.Error.CODE_TO_STATE_[code] || bot.Error.State.UNKNOWN_ERROR;
  78. /** @override */
  79. this.message = opt_message || '';
  80. var name = this.state.replace(/((?:^|\s+)[a-z])/g, function(str) {
  81. // IE<9 does not support String#trim(). Also, IE does not include 0xa0
  82. // (the non-breaking-space) in the \s character class, so we have to
  83. // explicitly include it.
  84. return str.toUpperCase().replace(/^[\s\xa0]+/g, '');
  85. });
  86. var l = name.length - 'Error'.length;
  87. if (l < 0 || name.indexOf('Error', l) != l) {
  88. name += 'Error';
  89. }
  90. /** @override */
  91. this.name = name;
  92. // Generate a stacktrace for our custom error; ensure the error has our
  93. // custom name and message so the stack prints correctly in all browsers.
  94. var template = new Error(this.message);
  95. template.name = this.name;
  96. /** @override */
  97. this.stack = template.stack || '';
  98. };
  99. goog.inherits(bot.Error, Error);
  100. /**
  101. * Status strings enumerated in the W3C WebDriver working draft.
  102. * @enum {string}
  103. * @see http://www.w3.org/TR/webdriver/#status-codes
  104. */
  105. bot.Error.State = {
  106. ELEMENT_NOT_SELECTABLE: 'element not selectable',
  107. ELEMENT_NOT_VISIBLE: 'element not visible',
  108. IME_ENGINE_ACTIVATION_FAILED: 'ime engine activation failed',
  109. IME_NOT_AVAILABLE: 'ime not available',
  110. INVALID_COOKIE_DOMAIN: 'invalid cookie domain',
  111. INVALID_ELEMENT_COORDINATES: 'invalid element coordinates',
  112. INVALID_ELEMENT_STATE: 'invalid element state',
  113. INVALID_SELECTOR: 'invalid selector',
  114. JAVASCRIPT_ERROR: 'javascript error',
  115. MOVE_TARGET_OUT_OF_BOUNDS: 'move target out of bounds',
  116. NO_SUCH_ALERT: 'no such alert',
  117. NO_SUCH_DOM: 'no such dom',
  118. NO_SUCH_ELEMENT: 'no such element',
  119. NO_SUCH_FRAME: 'no such frame',
  120. NO_SUCH_WINDOW: 'no such window',
  121. SCRIPT_TIMEOUT: 'script timeout',
  122. SESSION_NOT_CREATED: 'session not created',
  123. STALE_ELEMENT_REFERENCE: 'stale element reference',
  124. SUCCESS: 'success',
  125. TIMEOUT: 'timeout',
  126. UNABLE_TO_SET_COOKIE: 'unable to set cookie',
  127. UNEXPECTED_ALERT_OPEN: 'unexpected alert open',
  128. UNKNOWN_COMMAND: 'unknown command',
  129. UNKNOWN_ERROR: 'unknown error',
  130. UNSUPPORTED_OPERATION: 'unsupported operation'
  131. };
  132. /**
  133. * A map of error codes to state string.
  134. * @private {!Object.<bot.ErrorCode, bot.Error.State>}
  135. */
  136. bot.Error.CODE_TO_STATE_ = {};
  137. goog.scope(function() {
  138. var map = bot.Error.CODE_TO_STATE_;
  139. var code = bot.ErrorCode;
  140. var state = bot.Error.State;
  141. map[code.ELEMENT_NOT_SELECTABLE] = state.ELEMENT_NOT_SELECTABLE;
  142. map[code.ELEMENT_NOT_VISIBLE] = state.ELEMENT_NOT_VISIBLE;
  143. map[code.IME_ENGINE_ACTIVATION_FAILED] = state.IME_ENGINE_ACTIVATION_FAILED;
  144. map[code.IME_NOT_AVAILABLE] = state.IME_NOT_AVAILABLE;
  145. map[code.INVALID_COOKIE_DOMAIN] = state.INVALID_COOKIE_DOMAIN;
  146. map[code.INVALID_ELEMENT_COORDINATES] = state.INVALID_ELEMENT_COORDINATES;
  147. map[code.INVALID_ELEMENT_STATE] = state.INVALID_ELEMENT_STATE;
  148. map[code.INVALID_SELECTOR_ERROR] = state.INVALID_SELECTOR;
  149. map[code.INVALID_XPATH_SELECTOR] = state.INVALID_SELECTOR;
  150. map[code.INVALID_XPATH_SELECTOR_RETURN_TYPE] = state.INVALID_SELECTOR;
  151. map[code.JAVASCRIPT_ERROR] = state.JAVASCRIPT_ERROR;
  152. map[code.METHOD_NOT_ALLOWED] = state.UNSUPPORTED_OPERATION;
  153. map[code.MOVE_TARGET_OUT_OF_BOUNDS] = state.MOVE_TARGET_OUT_OF_BOUNDS;
  154. map[code.NO_MODAL_DIALOG_OPEN] = state.NO_SUCH_ALERT;
  155. map[code.NO_SUCH_ELEMENT] = state.NO_SUCH_ELEMENT;
  156. map[code.NO_SUCH_FRAME] = state.NO_SUCH_FRAME;
  157. map[code.NO_SUCH_WINDOW] = state.NO_SUCH_WINDOW;
  158. map[code.SCRIPT_TIMEOUT] = state.SCRIPT_TIMEOUT;
  159. map[code.SESSION_NOT_CREATED] = state.SESSION_NOT_CREATED;
  160. map[code.STALE_ELEMENT_REFERENCE] = state.STALE_ELEMENT_REFERENCE;
  161. map[code.SUCCESS] = state.SUCCESS;
  162. map[code.TIMEOUT] = state.TIMEOUT;
  163. map[code.UNABLE_TO_SET_COOKIE] = state.UNABLE_TO_SET_COOKIE;
  164. map[code.MODAL_DIALOG_OPENED] = state.UNEXPECTED_ALERT_OPEN;
  165. map[code.UNKNOWN_ERROR] = state.UNKNOWN_ERROR;
  166. map[code.UNSUPPORTED_OPERATION] = state.UNKNOWN_COMMAND;
  167. }); // goog.scope
  168. /**
  169. * Flag used for duck-typing when this code is embedded in a Firefox extension.
  170. * This is required since an Error thrown in one component and then reported
  171. * to another will fail instanceof checks in the second component.
  172. * @type {boolean}
  173. */
  174. bot.Error.prototype.isAutomationError = true;
  175. if (goog.DEBUG) {
  176. /** @return {string} The string representation of this error. */
  177. bot.Error.prototype.toString = function() {
  178. return this.name + ': ' + this.message;
  179. };
  180. }