ErrorHandler.pm 4.1 KB

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