RemoteConnection.pm 5.0 KB

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