ErrorHandler.pm 5.3 KB

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