RemoteConnection.pm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Error;
  11. use Data::Dumper;
  12. use Selenium::Remote::ErrorHandler;
  13. sub new {
  14. my ($class, $remote_srvr, $port) = @_;
  15. my $self = {
  16. remote_server_addr => $remote_srvr,
  17. port => $port,
  18. };
  19. bless $self, $class or die "Can't bless $class: $!";
  20. # Try connecting to the Selenium RC port
  21. my $p = Net::Ping->new("tcp", 2);
  22. $p->port_number($self->{'port'});
  23. croak "Selenium RC server is not responding\n"
  24. unless $p->ping($self->{'remote_server_addr'});
  25. undef($p);
  26. return $self;
  27. }
  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. $content = $json->allow_nonref->utf8->encode($params);
  47. }
  48. # HTTP request
  49. my $ua = LWP::UserAgent->new;
  50. my $header =
  51. HTTP::Headers->new(Content_Type => 'application/json; charset=utf-8');
  52. $header->header('Accept' => 'application/json');
  53. my $request = HTTP::Request->new($method, $fullurl, $header, $content);
  54. #print Dumper($request);
  55. my $response = $ua->request($request);
  56. #return $response;
  57. return $self->_process_response($response);
  58. }
  59. sub _process_response {
  60. my ($self, $response) = @_;
  61. my $data; #returned data from server
  62. my $json = new JSON;
  63. if ($response->is_redirect) {
  64. return $self->request('GET', $response->header('location'));
  65. }
  66. elsif ($response->code == 404) {
  67. return "Command not implemented on the RC server.";
  68. }
  69. elsif ($response->code > 199) {
  70. my $decoded_json;
  71. if ($response->message ne 'No Content') {
  72. $decoded_json = $json->allow_nonref->utf8->decode($response->content);
  73. }
  74. return $self->_get_command_result($decoded_json);
  75. }
  76. else {
  77. return "Unrecognized server status = " . $response->code;
  78. }
  79. }
  80. # When a command is processed by the remote server & a result is sent back, it
  81. # also includes other relevant info. We strip those & just return the value we're
  82. # interested in. And if there is an error, ErrorHandler will handle it.
  83. sub _get_command_result {
  84. my ($self, $resp) = @_;
  85. my $error_handler = new Selenium::Remote::ErrorHandler;
  86. if (defined $resp) {
  87. if (ref $resp eq 'HASH') {
  88. if (defined $resp->{'status'} && $resp->{'status'} != 0) {
  89. return $error_handler->process_error($resp);
  90. }
  91. else {
  92. # For new session we need to grab the session id.
  93. if ((ref $resp->{'value'} eq 'HASH') &&
  94. ($resp->{'value'}->{'class'} eq 'org.openqa.selenium.remote.DesiredCapabilities')) {
  95. return $resp;
  96. }
  97. else {
  98. return $resp->{'value'};
  99. }
  100. }
  101. }
  102. else
  103. {
  104. return $resp;
  105. }
  106. }
  107. else {
  108. # If there is no value or status assume success
  109. return 1;
  110. }
  111. }
  112. 1;
  113. __END__
  114. =head1 SEE ALSO
  115. For more information about Selenium , visit the website at
  116. L<http://code.google.com/p/selenium/>.
  117. =head1 BUGS
  118. The Selenium issue tracking system is available online at
  119. L<http://code.google.com/p/selenium/issues/list>.
  120. =head1 AUTHOR
  121. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  122. =head1 LICENSE
  123. Copyright (c) 2010 Juniper Networks, Inc
  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.