1
0

RemoteConnection.pm 4.8 KB

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