Selenium/Remote/RemoteConnection.pm

filter: Statementstmt 90.6% 116 / 128 Branchbran 88.1% 37 / 42 Conditioncond 59.0% 13 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 90.9% 20 / 22 Podpod 100% 2 / 2 totaltotal 83.4% 191 / 229 SCAR 35.1
CC
31
Cov
86%
_process_response
23.9
request
11.0
check_status
4.0
CRAP 33.4
« Selenium/Remote/Spec.pm ↑ Summary Selenium/CanStartBinary/FindBinary.pm »
1 package Selenium::Remote::RemoteConnection;
2 29 $Selenium::Remote::RemoteConnection::VERSION = '1.49';
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #ABSTRACT: Connect to a selenium server
7  
8 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 29 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 29 ▶use Selenium::Remote::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 29 ▶use Scalar::Util qw{looks_like_number};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 29 has 'remote_server_addr' => ( is => 'rw', );
20  
21 29 has 'port' => ( is => 'rw', );
22  
23 has 'debug' => (
24 is => 'rw',
25 30 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
26 29 );
27  
28 has 'ua' => (
29 is => 'lazy',
30 1 ▶ builder => sub { return LWP::UserAgent->new; }
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine _build_ua: not called CC=1 SCAR=1.2
31 29 );
32  
33 has 'error_handler' => (
34 is => 'lazy',
35 3 ▶ builder => sub { return Selenium::Remote::ErrorHandler->new; }
Subroutine _build_error_handler: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
36 29 );
37  
38 29 with 'Selenium::Remote::Driver::CanSetWebdriverContext';
39  
40  
41 sub check_status {
42 10 ▶ my $self = shift;
Subroutine check_status: called CC=4 SCAR=13.9
Pod documented
43 10 my $status;
44  
45 try {
46 10 ▶ $status = $self->request( { method => 'GET', url => 'status' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48 catch {
49 0 ▶ croak "Could not connect to SeleniumWebDriver: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 10 };
51  
52 10 ▶ my $cmdOut = $status->{cmd_status} || '';
Condition50%1/2
$status->{'cmd_status'} || ''
l!l
100
MC/DC0%0/1
$status->{'cmd_status'} || ''
$status->{'cmd_status'}
Truth table50%1/2
A
A= $status->{'cmd_status'}
Aresult
11
00
53 10 ▶ if ( $cmdOut ne 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdOut ne "OK") 1 9 10
54  
55 # Could be grid, see if we can talk to it
56 1 $status = undef;
57 1 $status =
58 $self->request( { method => 'GET', url => 'grid/api/hub/status' } );
59 }
60  
61 10 ▶ unless ( $cmdOut eq 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdOut eq "OK") 1 9 10
62 1 croak "Selenium server did not return proper status";
63 }
64 }
65  
66  
67 sub request {
68 30 ▶ my ( $self, $resource, $params, $dont_process_response ) = @_;
Subroutine request: called CC=11 SCAR=24.0
Pod documented
69 30 my $method = $resource->{method};
70 30 my $url = $resource->{url};
71 30 ▶ my $no_content_success = $resource->{no_content_success} // 0;
Condition100%2/2
$resource->{"no_content_success"} // 0
l!l
1515
MC/DC100%1/1
$resource->{"no_content_success"} // 0
$resource->{"no_content_success"}
Truth table100%2/2
A
A= $resource->{"no_content_success"}
Aresult
11
00
72  
73 30 my $content = '';
74 30 my $fullurl = '';
75  
76 # Construct full url.
77 30 ▶ if ( $url =~ m/^http/g ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($url =~ /^http/g) { } 3 27 30
elsif ($url =~ m[^/]) { } 1 26 27
elsif ($url =~ /grid/g) { } 2 24 26
78 3 $fullurl = $url;
79 }
80 elsif ( $url =~ m/^\// ) {
81  
82 # This is used when we get a 302 Redirect with a Location header.
83 1 $fullurl =
84 "http://" . $self->remote_server_addr . ":" . $self->port . $url;
85 }
86 elsif ( $url =~ m/grid/g ) {
87 2 $fullurl =
88 "http://" . $self->remote_server_addr . ":" . $self->port . "/$url";
89 }
90 else {
91 24 $fullurl =
92 "http://"
93 . $self->remote_server_addr . ":"
94 . $self->port
95 . $self->wd_context_prefix . "/$url";
96 }
97  
98 30 ▶ if ( ( defined $params ) && $params ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and $params ne "") 16 14 30
Condition66%2/3
defined $params and $params ne ""
!ll&&!rl&&r
14016
MC/DC50%1/2
defined $params and $params ne ""
defined $params $params ne ""
Truth table66%2/3
A and B
A= defined $paramsB= $params ne ""
ABresult
0X0
100
111
99  
100 #WebDriver 3 shims
101 16 ▶ if ( $resource->{payload} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resource->{"payload"}) 1 15 16
102 1 foreach my $key ( keys( %{ $resource->{payload} } ) ) {
103 1 $params->{$key} = $resource->{payload}->{$key};
104 }
105 }
106  
107 16 my $json = JSON->new;
108 16 $json->allow_blessed;
109 16 $content = $json->allow_nonref->utf8->encode($params);
110 }
111  
112 30 ▶ print "REQ: $method, $fullurl, $content\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 30 30
113  
114 # HTTP request
115 30 my $header =
116 HTTP::Headers->new( Content_Type => 'application/json; charset=utf-8' );
117 30 $header->header( 'Accept' => 'application/json' );
118 30 my $request = HTTP::Request->new( $method, $fullurl, $header, $content );
119 30 my $response = $self->ua->request($request);
120 30 ▶ if ($dont_process_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dont_process_response) 1 29 30
121 1 return $response;
122 }
123 29 return $self->_process_response( $response, $no_content_success );
124 }
125  
126 sub _process_response {
127 230 ▶ my ( $self, $response, $no_content_success ) = @_;
Subroutine _process_response: called CC=18 SCAR=31.7
128 230 my $data; # server response 'value' that'll be returned to the user
129 230 my $json = JSON->new;
130  
131 230 ▶ if ( $response->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) { } 2 228 230
132 2 my $redirect = {
133 method => 'GET',
134 url => $response->header('location')
135 };
136 2 return $self->request($redirect);
137 }
138 else {
139 228 my $decoded_json = undef;
140 228 ▶ print "RES: " . $response->decoded_content . "\n\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 77 151 228
141  
142 228 ▶ if ( ( $response->message ne 'No Content' )
Branch: 1 branch
BranchTrueFalseTotal
if ($response->message ne "No Content" and $response->content ne "") 217 11 228
Condition66%2/3
$response->message ne "No Content" and $response->content ne ""
!ll&&!rl&&r
011217
MC/DC50%1/2
$response->message ne "No Content" and $response->content ne ""
$response->message ne "No Content" $response->content ne ""
Truth table66%2/3
A and B
A= $response->message ne "No Content"B= $response->content ne ""
ABresult
0X0
100
111
143 && ( $response->content ne '' ) )
144 {
145 217 ▶ if ( $response->content_type !~ m/json/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->content_type =~ /json/i) 2 215 217
146 2 $data->{'cmd_status'} = 'NOTOK';
147 $data->{'cmd_return'}->{message} =
148 2 'Server returned error message '
149 . $response->content
150 . ' instead of data';
151 2 return $data;
152 }
153 $decoded_json =
154 215 $json->allow_nonref(1)->utf8(1)->decode( $response->content );
155 215 $data->{'sessionId'} = $decoded_json->{'sessionId'};
156 }
157  
158 226 ▶ if ( $response->is_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_error) { } 18 208 226
elsif ($response->is_success) { } 208 0 208
159 18 $data->{'cmd_status'} = 'NOTOK';
160 18 ▶ if ( defined $decoded_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_json) { } 17 1 18
161 17 $data->{'cmd_return'} =
162 $self->error_handler->process_error($decoded_json);
163 }
164 else {
165 1 $data->{'cmd_return'} =
166 'Server returned error code '
167 . $response->code
168 . ' and no data';
169 }
170 18 return $data;
171 }
172 elsif ( $response->is_success ) {
173 208 $data->{'cmd_status'} = 'OK';
174 208 ▶ if ( defined $decoded_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_json) { } 198 10 208
175  
176 #XXX MS edge doesn't follow spec here either
177 198 ▶ if ( looks_like_number( $decoded_json->{status} )
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($decoded_json->{"status"}) and $decoded_json->{"status"} > 0 and $decoded_json->{"value"}{"message"}) 0 198 198
Condition50%3/6
&looks_like_number($decoded_json->{"status"}) and $decoded_json->{"status"} > 0 and $decoded_json->{"value"}{"message"}
!ll&&!rl&&r
19800
&looks_like_number($decoded_json->{"status"}) and $decoded_json->{"status"} > 0
!ll&&!rl&&r
91890
MC/DC0%0/3
&looks_like_number($decoded_json->{"status"}) and $decoded_json->{"status"} > 0 and $decoded_json->{"value"}{"message"}
&looks_like_number($decoded_json->{"status"}) $decoded_json->{"status"} > 0 $decoded_json->{"value"}{"message"}
Truth table50%2/4
A and B and C
A= &looks_like_number($decoded_json->{"status"})B= $decoded_json->{"status"} > 0C= $decoded_json->{"value"}{"message"}
ABCresult
0XX0
10X0
1100
1111
178 && $decoded_json->{status} > 0
179 && $decoded_json->{value}{message} )
180 {
181 0 $data->{cmd_status} = 'NOT OK';
182 0 $data->{cmd_return} = $decoded_json->{value};
183 0 return $data;
184 }
185  
186 #XXX shockingly, neither does InternetExplorerDriver
187 198 ▶ if ( ref $decoded_json eq 'HASH' && $decoded_json->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded_json eq "HASH" and $decoded_json->{"error"}) 0 198 198
Condition33%1/3
ref $decoded_json eq "HASH" and $decoded_json->{"error"}
!ll&&!rl&&r
01980
MC/DC0%0/2
ref $decoded_json eq "HASH" and $decoded_json->{"error"}
ref $decoded_json eq "HASH" $decoded_json->{"error"}
Truth table33%1/3
A and B
A= ref $decoded_json eq "HASH"B= $decoded_json->{"error"}
ABresult
0X0
100
111
188 0 $data->{cmd_status} = 'NOT OK';
189 0 $data->{cmd_return} = $decoded_json;
190 0 return $data;
191 }
192  
193 198 ▶ if ($no_content_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_content_success) { } 71 127 198
194 71 $data->{'cmd_return'} = 1;
195 }
196 else {
197 127 $data->{'cmd_return'} = $decoded_json->{'value'};
198 127 ▶ if ( ref( $data->{cmd_return} ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"cmd_return"} eq "HASH" and exists $data->{"cmd_return"}{"sessionId"}) 0 127 127
Condition66%2/3
ref $data->{"cmd_return"} eq "HASH" and exists $data->{"cmd_return"}{"sessionId"}
!ll&&!rl&&r
75520
MC/DC0%0/2
ref $data->{"cmd_return"} eq "HASH" and exists $data->{"cmd_return"}{"sessionId"}
ref $data->{"cmd_return"} eq "HASH" exists $data->{"cmd_return"}{"sessionId"}
Truth table66%2/3
A and B
A= ref $data->{"cmd_return"} eq "HASH"B= exists $data->{"cmd_return"}{"sessionId"}
ABresult
0X0
100
111
199 && exists $data->{cmd_return}->{sessionId} )
200 {
201 0 $data->{sessionId} = $data->{cmd_return}->{sessionId};
202 }
203 }
204 }
205 else {
206 10 $data->{'cmd_return'} =
207 'Server returned status code '
208 . $response->code
209 . ' but no data';
210 }
211 208 return $data;
212 }
213 else {
214 # No idea what the server is telling me, must be high
215 0 $data->{'cmd_status'} = 'NOTOK';
216 0 $data->{'cmd_return'} =
217 'Server returned status code '
218 . $response->code
219 . ' which I don\'t understand';
220 0 return $data;
221 }
222 }
223 }
224  
225 29 1;
226  
227 __END__
228  
229 =pod
230  
231 =encoding UTF-8
232  
233 =head1 NAME
234  
235 Selenium::Remote::RemoteConnection - Connect to a selenium server
236  
237 =head1 VERSION
238  
239 version 1.49
240  
241 =head1 SYNOPSIS
242  
243 my $driver = Selenium::Remote::Driver->new();
244 eval { $driver->remote_conn->check_status() };
245 die "do something to kick the server" if $@;
246  
247 =head1 DESCRIPTION
248  
249 You shouldn't really need to use this module unless debugging or checking connections when testing dangerous things.
250  
251 =head1 CONSTRUCTOR
252  
253 =head2 new(%parameters)
254  
255 Accepts 5 parameters:
256  
257 =over 4
258  
259 =item B<remote_server_addr> - address of selenium server
260  
261 =item B<port> - port of selenium server
262  
263 =item B<ua> - Useful to override with Test::LWP::UserAgent in unit tests
264  
265 =item B<debug> - Should be self-explanatory
266  
267 =item B<error_handler> - Defaults to Selenium::Remote::ErrorHandler.
268  
269 =back
270  
271 These can be set any time later by getter/setters with the same name.
272  
273 =head1 METHODS
274  
275 =head2 check_status
276  
277 Croaks unless the selenium server is responsive. Sometimes is useful to call in-between tests (the server CAN die on you...)
278  
279 =head2 request
280  
281 Make a request of the Selenium server. Mostly useful for debugging things going wrong with Selenium::Remote::Driver when not in normal operation.
282  
283 =head1 AUTHORS
284  
285 Current Maintainers:
286  
287 =over 4
288  
289 =item *
290  
291 George S. Baugh <george@troglodyne.net>
292  
293 =back
294  
295 Previous maintainers:
296  
297 =over 4
298  
299 =item *
300  
301 Daniel Gempesaw <gempesaw@gmail.com>
302  
303 =item *
304  
305 Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>
306  
307 =item *
308  
309 Luke Closs <cpan@5thplane.com>
310  
311 =item *
312  
313 Mark Stosberg <mark@stosberg.com>
314  
315 =back
316  
317 Original authors:
318  
319 =over 4
320  
321 =item *
322  
323 Aditya Ivaturi <ivaturi@gmail.com>
324  
325 =back
326  
327 =head1 COPYRIGHT AND LICENSE
328  
329 Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
330  
331 Copyright (c) 2014-2017 Daniel Gempesaw
332  
333 Copyright (c) 2018-2021 George S. Baugh
334  
335 Licensed under the Apache License, Version 2.0 (the "License");
336 you may not use this file except in compliance with the License.
337 You may obtain a copy of the License at
338  
339 http://www.apache.org/licenses/LICENSE-2.0
340  
341 Unless required by applicable law or agreed to in writing, software
342 distributed under the License is distributed on an "AS IS" BASIS,
343 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
344 See the License for the specific language governing permissions and
345 limitations under the License.
346  
347 =cut
« Selenium/Remote/Spec.pm ↑ Summary Selenium/CanStartBinary/FindBinary.pm »