RemoteConnection.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package Selenium::Remote::RemoteConnection;
  2. use strict;
  3. use warnings;
  4. use LWP::UserAgent;
  5. use HTTP::Headers;
  6. use HTTP::Request;
  7. use Net::Ping;
  8. use Carp qw(croak);
  9. use JSON;
  10. use Data::Dumper;
  11. use Selenium::Remote::ErrorHandler;
  12. sub new {
  13. my ($class, $remote_srvr, $port) = @_;
  14. my $self = {
  15. remote_server_addr => $remote_srvr,
  16. port => $port,
  17. };
  18. bless $self, $class or die "Can't bless $class: $!";
  19. my $status = eval {$self->request('GET','status');};
  20. croak "Could not connect to SeleniumWebDriver" if($@);
  21. if($status->{cmd_status} eq 'OK') {
  22. return $self;
  23. } else {
  24. croak "Selenium server did not return proper status";
  25. }
  26. }
  27. # This request method is tailored for Selenium RC server
  28. sub request {
  29. my ($self, $method, $url, $params) = @_;
  30. my $content = '';
  31. my $fullurl = '';
  32. # Construct full url.
  33. if ($url =~ m/^http/g) {
  34. $fullurl = $url;
  35. }
  36. else {
  37. $fullurl =
  38. "http://"
  39. . $self->{remote_server_addr} . ":"
  40. . $self->{port}
  41. . "/wd/hub/$url";
  42. }
  43. if ((defined $params) && $params ne '') {
  44. my $json = new JSON;
  45. $content = $json->allow_nonref->utf8->encode($params);
  46. }
  47. # HTTP request
  48. my $ua = LWP::UserAgent->new;
  49. my $header =
  50. HTTP::Headers->new(Content_Type => 'application/json; charset=utf-8');
  51. $header->header('Accept' => 'application/json');
  52. my $request = HTTP::Request->new($method, $fullurl, $header, $content);
  53. my $response = $ua->request($request);
  54. return $self->_process_response($response);
  55. }
  56. sub _process_response {
  57. my ($self, $response) = @_;
  58. my $data; # server response 'value' that'll be returned to the user
  59. my $json = new JSON;
  60. if ($response->is_redirect) {
  61. return $self->request('GET', $response->header('location'));
  62. }
  63. else {
  64. my $decoded_json = undef;
  65. if (($response->message ne 'No Content') && ($response->content ne '')) {
  66. $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
  67. $data->{'sessionId'} = $decoded_json->{'sessionId'};
  68. }
  69. if ($response->is_error) {
  70. my $error_handler = new Selenium::Remote::ErrorHandler;
  71. $data->{'cmd_status'} = 'NOTOK';
  72. if (defined $decoded_json) {
  73. $data->{'cmd_return'} = $error_handler->process_error($decoded_json);
  74. }
  75. else {
  76. $data->{'cmd_return'} = 'Server returned error code '.$response->code.' and no data';
  77. }
  78. return $data;
  79. }
  80. elsif ($response->is_success) {
  81. $data->{'cmd_status'} = 'OK';
  82. if (defined $decoded_json) {
  83. $data->{'cmd_return'} = $decoded_json->{'value'};
  84. }
  85. else {
  86. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' but no data';
  87. }
  88. return $data;
  89. }
  90. else {
  91. # No idea what the server is telling me, must be high
  92. $data->{'cmd_status'} = 'NOTOK';
  93. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' which I don\'t understand';
  94. return $data;
  95. }
  96. }
  97. }
  98. 1;
  99. __END__
  100. =head1 SEE ALSO
  101. For more information about Selenium , visit the website at
  102. L<http://code.google.com/p/selenium/>.
  103. =head1 BUGS
  104. The Selenium issue tracking system is available online at
  105. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  106. =head1 CURRENT MAINTAINER
  107. Gordon Child C<< <gchild@gordonchild.com> >>
  108. =head1 AUTHOR
  109. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  110. =head1 LICENSE
  111. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  112. Licensed under the Apache License, Version 2.0 (the "License");
  113. you may not use this file except in compliance with the License.
  114. You may obtain a copy of the License at
  115. http://www.apache.org/licenses/LICENSE-2.0
  116. Unless required by applicable law or agreed to in writing, software
  117. distributed under the License is distributed on an "AS IS" BASIS,
  118. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  119. See the License for the specific language governing permissions and
  120. limitations under the License.