ErrorHandler.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package Selenium::Remote::ErrorHandler;
  2. use Moo;
  3. use Carp qw(croak);
  4. # We're going to handle only codes that are errors.
  5. # http://code.google.com/p/selenium/wiki/JsonWireProtocol
  6. has STATUS_CODE => (
  7. is => 'lazy',
  8. builder => sub {
  9. return {
  10. 7 => {
  11. 'code' => 'NO_SUCH_ELEMENT',
  12. 'msg' =>
  13. 'An element could not be located on the page using the given search parameters.',
  14. },
  15. 8 => {
  16. 'code' => 'NO_SUCH_FRAME',
  17. 'msg' =>
  18. 'A request to switch to a frame could not be satisfied because the frame could not be found.',
  19. },
  20. 9 => {
  21. 'code' => 'UNKNOWN_COMMAND',
  22. 'msg' =>
  23. 'The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.',
  24. },
  25. 10 => {
  26. 'code' => 'STALE_ELEMENT_REFERENCE',
  27. 'msg' =>
  28. 'An element command failed because the referenced element is no longer attached to the DOM.',
  29. },
  30. 11 => {
  31. 'code' => 'ELEMENT_NOT_VISIBLE',
  32. 'msg' =>
  33. 'An element command could not be completed because the element is not visible on the page.',
  34. },
  35. 12 => {
  36. 'code' => 'INVALID_ELEMENT_STATE',
  37. 'msg' =>
  38. 'An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).',
  39. },
  40. 13 => {
  41. 'code' => 'UNKNOWN_ERROR',
  42. 'msg' =>
  43. 'An unknown server-side error occurred while processing the command.',
  44. },
  45. 15 => {
  46. 'code' => 'ELEMENT_IS_NOT_SELECTABLE',
  47. 'msg' =>
  48. 'An attempt was made to select an element that cannot be selected.',
  49. },
  50. 19 => {
  51. 'code' => 'XPATH_LOOKUP_ERROR',
  52. 'msg' =>
  53. 'An error occurred while searching for an element by XPath.',
  54. },
  55. 21 => {
  56. 'code' => 'Timeout',
  57. 'msg' =>
  58. 'An operation did not complete before its timeout expired.',
  59. },
  60. 23 => {
  61. 'code' => 'NO_SUCH_WINDOW',
  62. 'msg' =>
  63. 'A request to switch to a different window could not be satisfied because the window could not be found.',
  64. },
  65. 24 => {
  66. 'code' => 'INVALID_COOKIE_DOMAIN',
  67. 'msg' =>
  68. 'An illegal attempt was made to set a cookie under a different domain than the current page.',
  69. },
  70. 25 => {
  71. 'code' => 'UNABLE_TO_SET_COOKIE',
  72. 'msg' =>
  73. 'A request to set a cookie\'s value could not be satisfied.',
  74. },
  75. 26 => {
  76. 'code' => 'UNEXPECTED_ALERT_OPEN',
  77. 'msg' => 'A modal dialog was open, blocking this operation',
  78. },
  79. 27 => {
  80. 'code' => 'NO_ALERT_OPEN_ERROR',
  81. 'msg' =>
  82. 'An attempt was made to operate on a modal dialog when one was not open.',
  83. },
  84. 28 => {
  85. 'code' => 'SCRIPT_TIMEOUT',
  86. 'msg' =>
  87. 'A script did not complete before its timeout expired.',
  88. },
  89. 29 => {
  90. 'code' => 'INVALID_ELEMENT_COORDINATES',
  91. 'msg' =>
  92. 'The coordinates provided to an interactions operation are invalid.',
  93. },
  94. 30 => {
  95. 'code' => 'IME_NOT_AVAILABLE',
  96. 'msg' => 'IME was not available.',
  97. },
  98. 31 => {
  99. 'code' => 'IME_ENGINE_ACTIVATION_FAILED',
  100. 'msg' => 'An IME engine could not be started.',
  101. },
  102. 32 => {
  103. 'code' => 'INVALID_SELECTOR',
  104. 'msg' => 'Argument was an invalid selector (e.g. XPath/CSS).',
  105. },
  106. };
  107. }
  108. );
  109. # Instead of just returning the end user a server returned error code, we will
  110. # put a more human readable & usable error message & that is what this method
  111. # is going to do.
  112. sub process_error {
  113. my ($self, $resp) = @_;
  114. # TODO: Handle screen if it sent back with the response. Either we could
  115. # let the end user handle it or we can save it an image file at a temp
  116. # location & return the path.
  117. my $ret;
  118. $ret->{'stackTrace'} = $resp->{'value'}->{'stackTrace'};
  119. $ret->{'error'} = $self->STATUS_CODE->{$resp->{'status'}};
  120. $ret->{'message'} = $resp->{'value'}->{'message'};
  121. return $ret;
  122. }
  123. 1;
  124. __END__
  125. =pod
  126. =head1 NAME
  127. Selenium::Remote::ErrorHandler - Error handler for Selenium::Remote::Driver
  128. =head1 SEE ALSO
  129. For more information about Selenium , visit the website at
  130. L<http://code.google.com/p/selenium/>.
  131. =head1 BUGS
  132. The Selenium issue tracking system is available online at
  133. L<http://github.com/gempesaw/Selenium-Remote-Driver/issues>.
  134. =head1 CURRENT MAINTAINER
  135. Daniel Gempesaw C<< <gempesaw@gmail.com> >>