RemoteConnection.pm 4.4 KB

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