RemoteConnection.pm 4.8 KB

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