RemoteConnection.pm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # Try connecting to the Selenium RC port
  20. my $p = Net::Ping->new("tcp", 2);
  21. $p->port_number($self->{'port'});
  22. croak "Selenium RC server is not responding\n"
  23. unless $p->ping($self->{'remote_server_addr'});
  24. undef($p);
  25. return $self;
  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. # Even though there are multiple error codes returned, at this point we care
  57. # mainly about 404. We should add code to handle specific HTTP Response codes.
  58. sub _process_response {
  59. my ($self, $response) = @_;
  60. my $data; #returned data from server
  61. my $json = new JSON;
  62. if ($response->is_redirect) {
  63. return $self->request('GET', $response->header('location'));
  64. }
  65. elsif ($response->code == 404) {
  66. return "Command not implemented on the RC server.";
  67. }
  68. elsif ($response->code > 199) {
  69. my $decoded_json;
  70. if ($response->message ne 'No Content') {
  71. $decoded_json = $json->allow_nonref->utf8->decode($response->content);
  72. }
  73. return $self->_get_command_result($decoded_json);
  74. }
  75. else {
  76. return "Unrecognized server status = " . $response->code;
  77. }
  78. }
  79. # When a command is processed by the remote server & a result is sent back, it
  80. # also includes other relevant info. We strip those & just return the value we're
  81. # interested in. And if there is an error, ErrorHandler will handle it.
  82. sub _get_command_result {
  83. my ($self, $resp) = @_;
  84. my $error_handler = new Selenium::Remote::ErrorHandler;
  85. if (defined $resp) {
  86. if (ref $resp eq 'HASH') {
  87. if (defined $resp->{'status'} && $resp->{'status'} != 0) {
  88. return $error_handler->process_error($resp);
  89. }
  90. else {
  91. # For new session we need to grab the session id.
  92. if ((ref $resp->{'value'} eq 'HASH') &&
  93. ($resp->{'value'}->{'class'} eq 'org.openqa.selenium.remote.DesiredCapabilities')) {
  94. return $resp;
  95. }
  96. else {
  97. return $resp->{'value'};
  98. }
  99. }
  100. }
  101. else
  102. {
  103. return $resp;
  104. }
  105. }
  106. else {
  107. # If there is no value or status assume success
  108. return 1;
  109. }
  110. }
  111. 1;
  112. __END__
  113. =head1 SEE ALSO
  114. For more information about Selenium , visit the website at
  115. L<http://code.google.com/p/selenium/>.
  116. =head1 BUGS
  117. The Selenium issue tracking system is available online at
  118. L<http://code.google.com/p/selenium/issues/list>.
  119. =head1 AUTHOR
  120. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  121. =head1 LICENSE
  122. Copyright (c) 2010 Aditya Ivaturi
  123. Licensed under the Apache License, Version 2.0 (the "License");
  124. you may not use this file except in compliance with the License.
  125. You may obtain a copy of the License at
  126. http://www.apache.org/licenses/LICENSE-2.0
  127. Unless required by applicable law or agreed to in writing, software
  128. distributed under the License is distributed on an "AS IS" BASIS,
  129. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  130. See the License for the specific language governing permissions and
  131. limitations under the License.