Selenium/Remote/RemoteConnection.pm

filter: Statementstmt 90.7% 117 / 129 Branchbran 88.1% 37 / 42 Conditioncond 58.3% 14 / 24 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 90.9% 20 / 22 Podpod 100% 2 / 2 totaltotal 82.8% 193 / 233 CC 32 SCAR 24.2
CC
32
Cov
86%
_process_response
23.9
request
11.0
check_status
5.1
CRAP 11.3
« Selenium/CanStartBinary.pm ↑ Summary Selenium/ActionChains.pm »
1 package Selenium::Remote::RemoteConnection;
2 29 $Selenium::Remote::RemoteConnection::VERSION = '1.50';
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 0 ▶ builder => sub { return LWP::UserAgent->new; }
Subroutine _build_ua: called CC=1 SCAR=1.2
Subroutine __ANON__: 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 10 ▶sub check_status {
Subroutine check_status: called CC=5 SCAR=16.3
Pod documented
42 10 my $self = shift;
43 10 my $status;
44  
45 10 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 10 $status = $self->request( { method => 'GET', url => 'status' } );
47 }
48 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 croak "Could not connect to SeleniumWebDriver: $_";
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 ▶ my $cr = $status->{cmd_return} // '';
Condition50%1/2
$status->{"cmd_return"} // ""
l!l
10
MC/DC0%0/1
$status->{"cmd_return"} // ""
$status->{"cmd_return"}
Truth table50%1/2
A
A= $status->{"cmd_return"}
Aresult
11
00
63 1 croak "Selenium server did not return proper status: $cmdOut: $cr";
64 }
65 }
66  
67  
68 30 ▶sub request {
Subroutine request: called CC=11 SCAR=24.0
Pod documented
69 30 my ( $self, $resource, $params, $dont_process_response ) = @_;
70 30 my $method = $resource->{method};
71 30 my $url = $resource->{url};
72 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
73  
74 30 my $content = '';
75 30 my $fullurl = '';
76  
77 # Construct full url.
78 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
79 3 $fullurl = $url;
80 }
81 elsif ( $url =~ m/^\// ) {
82  
83 # This is used when we get a 302 Redirect with a Location header.
84 1 $fullurl =
85 "http://" . $self->remote_server_addr . ":" . $self->port . $url;
86 }
87 elsif ( $url =~ m/grid/g ) {
88 2 $fullurl =
89 "http://" . $self->remote_server_addr . ":" . $self->port . "/$url";
90 }
91 else {
92 24 $fullurl =
93 "http://"
94 . $self->remote_server_addr . ":"
95 . $self->port
96 . $self->wd_context_prefix . "/$url";
97 }
98  
99 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
100  
101 #WebDriver 3 shims
102 16 ▶ if ( $resource->{payload} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resource->{"payload"}) 1 15 16
103 1 foreach my $key ( keys( %{ $resource->{payload} } ) ) {
104 1 $params->{$key} = $resource->{payload}->{$key};
105 }
106 }
107  
108 16 my $json = JSON->new;
109 16 $json->allow_blessed;
110 16 $content = $json->allow_nonref->utf8->encode($params);
111 }
112  
113 30 ▶ print "REQ: $method, $fullurl, $content\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 30 30
114  
115 # HTTP request
116 30 my $header =
117 HTTP::Headers->new( Content_Type => 'application/json; charset=utf-8' );
118 30 $header->header( 'Accept' => 'application/json' );
119 30 my $request = HTTP::Request->new( $method, $fullurl, $header, $content );
120 30 my $response = $self->ua->request($request);
121 30 ▶ if ($dont_process_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dont_process_response) 1 29 30
122 1 return $response;
123 }
124 29 return $self->_process_response( $response, $no_content_success );
125 }
126  
127 230 ▶sub _process_response {
Subroutine _process_response: called CC=18 SCAR=31.7
128 230 my ( $self, $response, $no_content_success ) = @_;
129 230 my $data; # server response 'value' that'll be returned to the user
130 230 my $json = JSON->new;
131  
132 230 ▶ if ( $response->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) { } 2 228 230
133 2 my $redirect = {
134 method => 'GET',
135 url => $response->header('location')
136 };
137 2 return $self->request($redirect);
138 }
139 else {
140 228 my $decoded_json = undef;
141 228 ▶ print "RES: " . $response->decoded_content . "\n\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 77 151 228
142  
143 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
144 && ( $response->content ne '' ) )
145 {
146 217 ▶ if ( $response->content_type !~ m/json/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->content_type =~ /json/i) 2 215 217
147 2 $data->{'cmd_status'} = 'NOTOK';
148 $data->{'cmd_return'}->{message} =
149 2 'Server returned error message '
150 . $response->content
151 . ' instead of data';
152 2 return $data;
153 }
154 $decoded_json =
155 215 $json->allow_nonref(1)->utf8(1)->decode( $response->content );
156 215 $data->{'sessionId'} = $decoded_json->{'sessionId'};
157 }
158  
159 226 ▶ if ( $response->is_error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_error) { } 18 208 226
elsif ($response->is_success) { } 208 0 208
160 18 $data->{'cmd_status'} = 'NOTOK';
161 18 ▶ if ( defined $decoded_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_json) { } 17 1 18
162 17 $data->{'cmd_return'} =
163 $self->error_handler->process_error($decoded_json);
164 }
165 else {
166 1 $data->{'cmd_return'} =
167 'Server returned error code '
168 . $response->code
169 . ' and no data';
170 }
171 18 return $data;
172 }
173 elsif ( $response->is_success ) {
174 208 $data->{'cmd_status'} = 'OK';
175 208 ▶ if ( defined $decoded_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_json) { } 198 10 208
176  
177 #XXX MS edge doesn't follow spec here either
178 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
179 && $decoded_json->{status} > 0
180 && $decoded_json->{value}{message} )
181 {
182 0 $data->{cmd_status} = 'NOT OK';
183 0 $data->{cmd_return} = $decoded_json->{value};
184 0 return $data;
185 }
186  
187 #XXX shockingly, neither does InternetExplorerDriver
188 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
189 0 $data->{cmd_status} = 'NOT OK';
190 0 $data->{cmd_return} = $decoded_json;
191 0 return $data;
192 }
193  
194 198 ▶ if ($no_content_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_content_success) { } 71 127 198
195 71 $data->{'cmd_return'} = 1;
196 }
197 else {
198 127 $data->{'cmd_return'} = $decoded_json->{'value'};
199 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
200 && exists $data->{cmd_return}->{sessionId} )
201 {
202 0 $data->{sessionId} = $data->{cmd_return}->{sessionId};
203 }
204 }
205 }
206 else {
207 10 $data->{'cmd_return'} =
208 'Server returned status code '
209 . $response->code
210 . ' but no data';
211 }
212 208 return $data;
213 }
214 else {
215 # No idea what the server is telling me, must be high
216 0 $data->{'cmd_status'} = 'NOTOK';
217 0 $data->{'cmd_return'} =
218 'Server returned status code '
219 . $response->code
220 . ' which I don\'t understand';
221 0 return $data;
222 }
223 }
224 }
225  
226 29 1;
227  
228 __END__
« Selenium/CanStartBinary.pm ↑ Summary Selenium/ActionChains.pm »