RemoteConnection.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
  83. $data->{'sessionId'} = $decoded_json->{'sessionId'};
  84. }
  85. if ($response->is_error) {
  86. my $error_handler = new Selenium::Remote::ErrorHandler;
  87. $data->{'cmd_status'} = 'NOTOK';
  88. if (defined $decoded_json) {
  89. $data->{'cmd_return'} = $error_handler->process_error($decoded_json);
  90. }
  91. else {
  92. $data->{'cmd_return'} = 'Server returned error code '.$response->code.' and no data';
  93. }
  94. return $data;
  95. }
  96. elsif ($response->is_success) {
  97. $data->{'cmd_status'} = 'OK';
  98. if (defined $decoded_json) {
  99. $data->{'cmd_return'} = $decoded_json->{'value'};
  100. }
  101. else {
  102. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' but no data';
  103. }
  104. return $data;
  105. }
  106. else {
  107. # No idea what the server is telling me, must be high
  108. $data->{'cmd_status'} = 'NOTOK';
  109. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' which I don\'t understand';
  110. return $data;
  111. }
  112. }
  113. }
  114. 1;
  115. __END__
  116. =head1 SEE ALSO
  117. For more information about Selenium , visit the website at
  118. L<http://code.google.com/p/selenium/>.
  119. =head1 BUGS
  120. The Selenium issue tracking system is available online at
  121. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  122. =head1 CURRENT MAINTAINER
  123. Gordon Child C<< <gchild@gordonchild.com> >>
  124. =head1 AUTHOR
  125. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  126. =head1 LICENSE
  127. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  128. Licensed under the Apache License, Version 2.0 (the "License");
  129. you may not use this file except in compliance with the License.
  130. You may obtain a copy of the License at
  131. http://www.apache.org/licenses/LICENSE-2.0
  132. Unless required by applicable law or agreed to in writing, software
  133. distributed under the License is distributed on an "AS IS" BASIS,
  134. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  135. See the License for the specific language governing permissions and
  136. limitations under the License.