RemoteConnection.pm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package Selenium::Remote::RemoteConnection;
  2. #ABSTRACT: Connect to a selenium server
  3. use Moo;
  4. use Try::Tiny;
  5. use LWP::UserAgent;
  6. use HTTP::Headers;
  7. use HTTP::Request;
  8. use Net::Ping;
  9. use Carp qw(croak);
  10. use JSON;
  11. use Data::Dumper;
  12. use Selenium::Remote::ErrorHandler;
  13. has 'remote_server_addr' => (
  14. is => 'rw',
  15. );
  16. has 'port' => (
  17. is => 'rw',
  18. );
  19. has 'debug' => (
  20. is => 'rw',
  21. default => sub { 0 }
  22. );
  23. has 'ua' => (
  24. is => 'lazy',
  25. builder => sub { return LWP::UserAgent->new; }
  26. );
  27. has 'error_handler' => (
  28. is => 'lazy',
  29. builder => sub { return Selenium::Remote::ErrorHandler->new; }
  30. );
  31. sub check_status {
  32. my $self = shift;
  33. my $status;
  34. try {
  35. $status = $self->request({method => 'GET', url => 'status'});
  36. }
  37. catch {
  38. croak "Could not connect to SeleniumWebDriver: $_" ;
  39. };
  40. if($status->{cmd_status} ne 'OK') {
  41. # Could be grid, see if we can talk to it
  42. $status = undef;
  43. $status = $self->request({method => 'GET', url => 'grid/api/hub/status'});
  44. }
  45. unless ($status->{cmd_status} eq 'OK') {
  46. croak "Selenium server did not return proper status";
  47. }
  48. }
  49. # This request method is tailored for Selenium RC server
  50. sub request {
  51. my ($self,$resource,$params,$dont_process_response) = @_;
  52. my $method = $resource->{method};
  53. my $url = $resource->{url};
  54. my $no_content_success = $resource->{no_content_success} // 0;
  55. my $content = '';
  56. my $fullurl = '';
  57. # Construct full url.
  58. if ($url =~ m/^http/g) {
  59. $fullurl = $url;
  60. }
  61. elsif ($url =~ m/grid/g) {
  62. $fullurl =
  63. "http://"
  64. . $self->remote_server_addr . ":"
  65. . $self->port
  66. . "/$url";
  67. }
  68. else {
  69. $fullurl =
  70. "http://"
  71. . $self->remote_server_addr . ":"
  72. . $self->port
  73. . "/wd/hub/$url";
  74. }
  75. if ((defined $params) && $params ne '') {
  76. my $json = JSON->new;
  77. $json->allow_blessed;
  78. $content = $json->allow_nonref->utf8->encode($params);
  79. }
  80. print "REQ: $method, $url, $content\n" if $self->debug;
  81. # HTTP request
  82. my $header =
  83. HTTP::Headers->new(Content_Type => 'application/json; charset=utf-8');
  84. $header->header('Accept' => 'application/json');
  85. my $request = HTTP::Request->new($method, $fullurl, $header, $content);
  86. my $response = $self->ua->request($request);
  87. if ($dont_process_response) {
  88. return $response;
  89. }
  90. return $self->_process_response($response, $no_content_success);
  91. }
  92. sub _process_response {
  93. my ($self, $response, $no_content_success) = @_;
  94. my $data; # server response 'value' that'll be returned to the user
  95. my $json = JSON->new;
  96. if ($response->is_redirect) {
  97. my $redirect = {
  98. method => 'GET',
  99. url => $response->header('location')
  100. };
  101. return $self->request($redirect);
  102. }
  103. else {
  104. my $decoded_json = undef;
  105. print "RES: ".$response->decoded_content."\n\n" if $self->debug;
  106. if (($response->message ne 'No Content') && ($response->content ne '')) {
  107. if ($response->content_type !~ m/json/i) {
  108. $data->{'cmd_status'} = 'NOTOK';
  109. $data->{'cmd_return'}->{message} = 'Server returned error message '.$response->content.' instead of data';
  110. return $data;
  111. }
  112. $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
  113. $data->{'sessionId'} = $decoded_json->{'sessionId'};
  114. }
  115. if ($response->is_error) {
  116. $data->{'cmd_status'} = 'NOTOK';
  117. if (defined $decoded_json) {
  118. $data->{'cmd_return'} = $self->error_handler->process_error($decoded_json);
  119. }
  120. else {
  121. $data->{'cmd_return'} = 'Server returned error code '.$response->code.' and no data';
  122. }
  123. return $data;
  124. }
  125. elsif ($response->is_success) {
  126. $data->{'cmd_status'} = 'OK';
  127. if (defined $decoded_json) {
  128. if ($no_content_success) {
  129. $data->{'cmd_return'} = 1
  130. }
  131. else {
  132. $data->{'cmd_return'} = $decoded_json->{'value'};
  133. }
  134. }
  135. else {
  136. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' but no data';
  137. }
  138. return $data;
  139. }
  140. else {
  141. # No idea what the server is telling me, must be high
  142. $data->{'cmd_status'} = 'NOTOK';
  143. $data->{'cmd_return'} = 'Server returned status code '.$response->code.' which I don\'t understand';
  144. return $data;
  145. }
  146. }
  147. }
  148. 1;
  149. __END__