RemoteConnection.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. 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. sub request {
  28. my ($self, $method, $url, $params) = @_;
  29. my $content = '';
  30. my $fullurl = '';
  31. # Construct full url.
  32. if ($url =~ m/^http/g) {
  33. $fullurl = $url;
  34. }
  35. else {
  36. $fullurl =
  37. "http://"
  38. . $self->{remote_server_addr} . ":"
  39. . $self->{port}
  40. . "/wd/hub/$url";
  41. }
  42. if ((defined $params) && $params ne '') {
  43. my $json = new JSON;
  44. #$content = "[" . $json->allow_nonref->utf8->encode($params) . "]";
  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. #print Dumper($request);
  54. my $response = $ua->request($request);
  55. #return $response;
  56. return $self->_process_response($response);
  57. }
  58. sub _process_response {
  59. my ($self, $response) = @_;
  60. my $data; #returned data from server
  61. if ($response->is_redirect) {
  62. return $self->request('GET', $response->header('location'));
  63. }
  64. elsif (($response->is_success) && ($response->code == 200)) {
  65. $data = from_json($response->content);
  66. if ($data->{'status'} != 0) {
  67. croak "Error occurred in server while processing request: $data";
  68. }
  69. return $data;
  70. }
  71. elsif ( ($response->is_success)
  72. && (($response->code == 200) || ($response->code == 204))) {
  73. # Nothing to do.
  74. }
  75. elsif ($response->code == 404) {
  76. croak "No such command.";
  77. }
  78. else {
  79. croak "Remote server error with status = " . $response->code;
  80. }
  81. }
  82. 1;
  83. __END__
  84. =head1 SEE ALSO
  85. For more information about Selenium , visit the website at
  86. L<http://code.google.com/p/selenium/>.
  87. =head1 BUGS
  88. The Selenium issue tracking system is available online at
  89. L<http://code.google.com/p/selenium/issues/list>.
  90. =head1 AUTHOR
  91. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  92. =head1 LICENSE
  93. Copyright (c) 2010 Juniper Networks, Inc
  94. Licensed under the Apache License, Version 2.0 (the "License");
  95. you may not use this file except in compliance with the License.
  96. You may obtain a copy of the License at
  97. http://www.apache.org/licenses/LICENSE-2.0
  98. Unless required by applicable law or agreed to in writing, software
  99. distributed under the License is distributed on an "AS IS" BASIS,
  100. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  101. See the License for the specific language governing permissions and
  102. limitations under the License.