Selenium/CanStartBinary.pm

filter: Statementstmt 37.1% 55 / 148 Branchbran 12.5% 6 / 48 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 36.3% 12 / 33 Podpod 0.0% 0 / 5 totaltotal 28.4% 75 / 264 CC 34 SCAR 24.2
CC
34
Cov
29%
_build_binary_mode
42.0
_handle_firefox_setup
30.0
shutdown_unix_binary
20.0
CRAP 11.3
« Selenium/Remote/Spec.pm ↑ Summary Selenium/Remote/RemoteConnection.pm »
1 package Selenium::CanStartBinary;
2 10 $Selenium::CanStartBinary::VERSION = '1.50';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
7 10 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use Selenium::CanStartBinary::ProbePort
9 10 ▶ qw/find_open_port_above find_open_port probe_port/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Selenium::Firefox::Binary qw/setup_firefox_binary_env/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Selenium::Waiter qw/wait_until/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use constant IS_WIN => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 10 requires 'binary';
18  
19  
20 10 requires 'binary_port';
21  
22  
23 10 requires '_binary_args';
24  
25  
26 has '_real_binary' => (
27 is => 'lazy',
28 0 ▶ builder => sub {
Subroutine _build__real_binary: not called CC=2 SCAR=17.9
29 0 my ($self) = @_;
30  
31 0 ▶ if ( $self->_is_old_ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_old_ff) { } 0 0 0
32 0 return $self->firefox_binary;
33 }
34 else {
35 0 return $self->binary;
36 }
37 }
38 10 );
39  
40 has '_is_old_ff' => (
41 is => 'lazy',
42 0 ▶ builder => sub {
Subroutine _build__is_old_ff: not called CC=2 SCAR=17.9
Subroutine __ANON__: not called CC=2 SCAR=17.9
43 0 my ($self) = @_;
44  
45 0 ▶ return $self->isa('Selenium::Firefox') && !$self->marionette_enabled;
Condition0%0/6
$self->isa('Selenium::Firefox') && !$self->marionette_enabled
!ll&&!rl&&r
000
$self->isa('Selenium::Firefox') && !$self->marionette_enabled
!ll&&!rl&&r
000
MC/DC0%0/2
$self->isa('Selenium::Firefox') && !$self->marionette_enabled
$self->isa('Selenium::Firefox') !$self->marionette_enabled
MC/DC0%0/2
$self->isa('Selenium::Firefox') && !$self->marionette_enabled
$self->isa('Selenium::Firefox') !$self->marionette_enabled
Truth table0%0/3
A && not(B)
A= $self->isa('Selenium::Firefox')B= !$self->marionette_enabled
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $self->isa('Selenium::Firefox')B= !$self->marionette_enabled
ABresult
0X0
100
111
46 }
47 10 );
48  
49 has '+port' => (
50 is => 'lazy',
51 0 ▶ builder => sub {
Subroutine _build_port: not called CC=3 SCAR=24.8
Subroutine __ANON__: not called CC=3 SCAR=24.8
52 0 my ($self) = @_;
53  
54 0 ▶ if ( $self->_real_binary ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_real_binary) { } 0 0 0
if ($self->_real_binary) { } 0 0 0
55 0 ▶ if ( $self->fixed_ports ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->fixed_ports) { } 0 0 0
if ($self->fixed_ports) { } 0 0 0
56 0 return find_open_port( $self->binary_port );
57 }
58 else {
59 0 return find_open_port_above( $self->binary_port );
60 }
61 }
62 else {
63 0 return 4444;
64 }
65 }
66 10 );
67  
68  
69 has 'fixed_ports' => (
70 is => 'lazy',
71 0 ▶ default => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 10 );
73  
74  
75 has custom_args => (
76 is => 'lazy',
77 predicate => 1,
78 0 ▶ default => sub { '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 10 );
80  
81 has 'marionette_port' => (
82 is => 'lazy',
83 0 ▶ builder => sub {
Subroutine _build_marionette_port: not called CC=3 SCAR=24.8
84 0 my ($self) = @_;
85  
86 0 ▶ if ( $self->_is_old_ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_old_ff) { } 0 0 0
87 0 return 0;
88 }
89 else {
90 0 ▶ if ( $self->fixed_ports ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fixed_ports) { } 0 0 0
91 0 return find_open_port( $self->marionette_binary_port );
92 }
93 else {
94 0 return find_open_port_above( $self->marionette_binary_port );
95 }
96 }
97 }
98 10 );
99  
100  
101 has startup_timeout => (
102 is => 'lazy',
103 0 ▶ default => sub { 10 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 10 );
105  
106  
107 10 has 'binary_mode' => (
108 is => 'lazy',
109 init_arg => undef,
110 builder => 1,
111 predicate => 1
112 );
113  
114 has 'try_binary' => (
115 is => 'lazy',
116 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 4 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
118 4 my ($self) = @_;
119 4 ▶ $self->binary_mode if $self->try_binary;
Branch: 1 branch
BranchTrueFalseTotal
if $self->try_binary 0 4 4
120 }
121 10 );
122  
123  
124 has 'window_title' => (
125 is => 'lazy',
126 init_arg => undef,
127 0 ▶ builder => sub {
Subroutine _build_window_title: not called CC=1 SCAR=6.9
128 0 my ($self) = @_;
129 0 my ( undef, undef, $file ) =
130 File::Spec->splitpath( $self->_real_binary );
131 0 my $port = $self->port;
132  
133 0 return $file . ':' . $port;
134 }
135 10 );
136  
137  
138 has '_command' => (
139 is => 'lazy',
140 init_arg => undef,
141 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 my ($self) = @_;
143 0 return $self->_construct_command;
144 }
145 10 );
146  
147  
148 has 'logfile' => (
149 is => 'lazy',
150 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 return '/nul' if IS_WIN;
152 0 return '/dev/null';
153 }
154 10 );
155  
156 4 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=13.0
Pod undocumented
157  
158 # There's a bit of finagling to do to since we can't ensure the
159 # attribute instantiation order. To decide whether we're going into
160 # binary mode, we need the remote_server_addr and port. But, they're
161 # both lazy and only instantiated immediately before S:R:D's
162 # remote_conn attribute. Once remote_conn is set, we can't change it,
163 # so we need the following order:
164 #
165 # parent: remote_server_addr, port
166 # role: binary_mode (aka _build_binary_mode)
167 # parent: remote_conn
168 #
169 # Since we can't force an order, we introduced try_binary which gets
170 # decided during BUILDARGS to tip us off as to whether we should try
171 # binary mode or not.
172 4 my ( undef, %args ) = @_;
173  
174 4 ▶ if ( !exists $args{remote_server_addr} && !exists $args{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'remote_server_addr'} || exists $args{'port'}) { } 0 4 4
Condition33%1/3
exists $args{'remote_server_addr'} || exists $args{'port'}
l!l&&r!l&&!r
400
MC/DC0%0/2
exists $args{'remote_server_addr'} || exists $args{'port'}
exists $args{'remote_server_addr'} exists $args{'port'}
Truth table33%1/3
A || B
A= exists $args{'remote_server_addr'}B= exists $args{'port'}
ABresult
1X1
011
000
175 0 $args{try_binary} = 1;
176  
177 # Windows may throw a fit about invalid pointers if we try to
178 # connect to localhost instead of 127.1
179 0 $args{remote_server_addr} = '127.0.0.1';
180 }
181 else {
182 4 $args{try_binary} = 0;
183 4 $args{binary_mode} = 0;
184 }
185  
186 4 return {%args};
187 }
188  
189 0 ▶sub _build_binary_mode {
Subroutine _build_binary_mode: not called CC=6 SCAR=37.4
190 0 my ($self) = @_;
191  
192 # We don't know what to do without a binary driver to start up
193 0 ▶ return unless $self->_real_binary;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_real_binary 0 0 0
194  
195 # Either the user asked for 4444, or we couldn't find an open port
196 0 my $port = $self->port + 0;
197 0 ▶ return if $port == 4444;
Branch: 1 branch
BranchTrueFalseTotal
if $port == 4444 0 0 0
198 0 ▶ if ( $self->fixed_ports && $port == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fixed_ports and $port == 0) 0 0 0
Condition0%0/3
$self->fixed_ports and $port == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->fixed_ports and $port == 0
$self->fixed_ports $port == 0
Truth table0%0/3
A and B
A= $self->fixed_portsB= $port == 0
ABresult
0X0
100
111
199 0 die 'port '
200 . $self->binary_port
201 . ' is not free and have requested fixed ports';
202 }
203  
204 0 $self->_handle_firefox_setup($port);
205  
206 0 system( $self->_command );
207  
208 my $success =
209 0 ▶ wait_until { probe_port($port) } timeout => $self->startup_timeout;
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
211 0 return 1;
212 }
213 else {
214 0 die 'Unable to connect to the '
215 . $self->_real_binary
216 . ' binary on port '
217 . $port;
218 }
219 }
220  
221 0 ▶sub _handle_firefox_setup {
Subroutine _handle_firefox_setup: not called CC=5 SCAR=34.0
222 0 my ( $self, $port ) = @_;
223  
224 # This is a no-op for other browsers
225 0 ▶ return unless $self->isa('Selenium::Firefox');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Selenium::Firefox") 0 0 0
226  
227 0 ▶ my $user_profile =
Branch: 1 branch
BranchTrueFalseTotal
$self->has_firefox_profile ? : 0 0 0
228 $self->has_firefox_profile
229 ? $self->firefox_profile
230 : 0;
231  
232 0 my $profile =
233 setup_firefox_binary_env( $port, $self->marionette_port, $user_profile );
234  
235 0 ▶ if ( $self->_is_old_ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_old_ff) { } 0 0 0
236  
237 # For non-geckodriver/non-marionette, we want to get rid of
238 # the profile so that we don't accidentally zip it and encode
239 # it down the line while Firefox is trying to read from it.
240 0 ▶ $self->clear_firefox_profile if $self->has_firefox_profile;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_firefox_profile 0 0 0
241 }
242 else {
243 # For geckodriver/marionette, we keep the enhanced profile around because
244 # we need to send it to geckodriver as a zipped b64-encoded
245 # directory.
246 0 $self->firefox_profile($profile);
247 }
248 }
249  
250 4 ▶sub shutdown_binary {
Subroutine shutdown_binary: called CC=5 SCAR=22.9
Pod undocumented
251 4 my ($self) = @_;
252  
253 4 ▶ return unless $self->auto_close();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_close 0 4 4
254 4 ▶ if ( defined $self->session_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->session_id) 0 4 4
255 0 $self->quit();
256 }
257 4 ▶ if ( $self->has_binary_mode && $self->binary_mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_binary_mode and $self->binary_mode) 0 4 4
Condition33%1/3
$self->has_binary_mode and $self->binary_mode
!ll&&!rl&&r
400
MC/DC0%0/2
$self->has_binary_mode and $self->binary_mode
$self->has_binary_mode $self->binary_mode
Truth table33%1/3
A and B
A= $self->has_binary_modeB= $self->binary_mode
ABresult
0X0
100
111
258  
259 # Tell the binary itself to shutdown
260 0 my $port = $self->port;
261 0 my $ua = $self->ua;
262 0 $ua->get( 'http://127.0.0.1:' . $port . '/wd/hub/shutdown' );
263  
264 # Close the orphaned command windows on windows
265 0 $self->shutdown_windows_binary;
266 0 $self->shutdown_unix_binary;
267 }
268  
269 }
270  
271 0 ▶sub shutdown_unix_binary {
Subroutine shutdown_unix_binary: not called CC=4 SCAR=30.0
Pod undocumented
272 0 my ($self) = @_;
273 0 if (!IS_WIN) {
274 0 my $cmd = "lsof -t -i :".$self->port();
275 0 ▶ my ( $pid ) = grep { $_ && $_ ne $$ } split( /\s+/, scalar `$cmd` );
Condition0%0/3
$_ && $_ ne $$
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && $_ ne $$
$_ $_ ne $$
Truth table0%0/3
A && B
A= $_B= $_ ne $$
ABresult
0X0
100
111
276 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
277 0 print "Killing Driver PID $pid listening on port "
278 . $self->port . "...\n";
279 0 eval { kill 'KILL', $pid };
280 0 ▶ warn
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
281 "Could not kill driver process! you may have to clean up manually."
282 if $@;
283 }
284 }
285 }
286  
287 0 ▶sub shutdown_windows_binary {
Subroutine shutdown_windows_binary: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my ($self) = @_;
289  
290 0 if (IS_WIN) {
291 if ( $self->_is_old_ff ) {
292  
293 # FIXME: Blech, handle a race condition that kills the
294 # driver before it's finished cleaning up its sessions. In
295 # particular, when the perl process ends, it wants to
296 # clean up the temp directory it created for the Firefox
297 # profile. But, if the Firefox process is still running,
298 # it will have a lock on the temp profile directory, and
299 # perl will get upset. This "solution" is _very_ bad.
300 sleep(2);
301  
302 # Firefox doesn't have a Driver/Session architecture - the
303 # only thing running is Firefox itself, so there's no
304 # other task to kill.
305 return;
306 }
307 system( 'taskkill /FI "WINDOWTITLE eq '
308 . $self->window_title
309 . '" > nul 2>&1' );
310 }
311 }
312  
313 4 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.1
Pod undocumented
314 4 my ( $self, $in_gd ) = @_;
315  
316 # if we're in global destruction, all bets are off.
317 4 ▶ return if $in_gd;
Branch: 1 branch
BranchTrueFalseTotal
if $in_gd 0 4 4
318 4 $self->shutdown_binary;
319 }
320  
321 0 ▶sub _construct_command {
Subroutine _construct_command: not called CC=2 SCAR=17.9
322 0 my ($self) = @_;
323 0 my $executable = $self->_real_binary;
324  
325 # Executable path names may have spaces
326 0 $executable = '"' . $executable . '"';
327  
328 # The different binaries take different arguments for proper setup
329 0 $executable .= $self->_binary_args;
330 0 ▶ if ( $self->has_custom_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_custom_args) 0 0 0
331 0 $executable .= ' ' . $self->custom_args;
332 }
333  
334 # Handle Windows vs Unix discrepancies for invoking shell commands
335 0 my ( $prefix, $suffix ) = ( $self->_cmd_prefix, $self->_cmd_suffix );
336 0 return join( ' ', ( $prefix, $executable, $suffix ) );
337 }
338  
339 0 ▶sub _cmd_prefix {
Subroutine _cmd_prefix: not called CC=1 SCAR=6.9
340 0 my ($self) = @_;
341  
342 0 my $prefix = '';
343 0 if (IS_WIN) {
344 $prefix = 'start "' . $self->window_title . '"';
345  
346 if ( $self->_is_old_ff ) {
347  
348 # For older versions of Firefox that run without
349 # marionette, the command we're running actually starts up
350 # the browser itself, so we don't want to minimize it.
351 return $prefix;
352 }
353 else {
354 # If we're firefox with marionette, or any other browser,
355 # the command we're running is the driver, and we don't
356 # need want the command window in the foreground.
357 return $prefix . ' /MIN ';
358 }
359 }
360 0 return $prefix;
361 }
362  
363 0 ▶sub _cmd_suffix {
Subroutine _cmd_suffix: not called CC=1 SCAR=6.9
364 0 my ($self) = @_;
365 0 return " > " . $self->logfile . " 2>&1 " if IS_WIN;
366 0 return " > " . $self->logfile . " 2>&1 &";
367 }
368  
369  
370 10 1;
371  
372 __END__
« Selenium/Remote/Spec.pm ↑ Summary Selenium/Remote/RemoteConnection.pm »