File Coverage

blib/lib/Selenium/Firefox/Binary.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 12 0.0
condition 0 15 0.0
subroutine 4 9 44.4
pod 2 2 100.0
total 18 84 21.4


line stmt bran cond sub pod time code
1             package Selenium::Firefox::Binary;
2             $Selenium::Firefox::Binary::VERSION = '1.50';
3 12     12   260923 use strict;
  12         26  
  12         467  
4 12     12   59 use warnings;
  12         22  
  12         750  
5              
6             # ABSTRACT: Subroutines for locating and properly initializing the Firefox Binary
7 12     12   2153 use File::Which qw/which/;
  12         5852  
  12         771  
8 12     12   5901 use Selenium::Firefox::Profile;
  12         51  
  12         7291  
9              
10             require Exporter;
11             our @ISA = qw/Exporter/;
12             our @EXPORT_OK = qw/firefox_path setup_firefox_binary_env/;
13              
14             sub _firefox_windows_path {
15              
16             # TODO: make this slightly less dumb
17             my @program_files = (
18             $ENV{PROGRAMFILES} // 'C:\Program Files',
19 0   0 0     $ENV{'PROGRAMFILES(X86)'} // 'C:\Program Files (x86)',
      0        
20             );
21              
22 0           foreach (@program_files) {
23 0           my $binary_path = $_ . '\Mozilla Firefox\firefox.exe';
24 0 0         return $binary_path if -x $binary_path;
25             }
26              
27             # Fall back to a completely naive strategy
28             warn
29 0           q/We couldn't find a viable firefox.EXE; you may want to specify it via the binary attribute./;
30 0           return which('firefox');
31             }
32              
33             sub _firefox_darwin_path {
34 0     0     my $default_firefox =
35             '/Applications/Firefox.app/Contents/MacOS/firefox';
36 0           my $default_firefox_bin =
37             '/Applications/Firefox.app/Contents/MacOS/firefox-bin';
38              
39 0           for my $path ( $default_firefox_bin, $default_firefox ) {
40 0 0 0       if ( -e $path && -x $path ) {
41 0           return $path;
42             }
43             }
44 0   0       return which('firefox-bin') || which('firefox');
45             }
46              
47             sub _firefox_unix_path {
48              
49             # TODO: maybe which('firefox3'), which('firefox2') ?
50 0   0 0     return which('firefox') || '/usr/bin/firefox';
51             }
52              
53              
54             sub firefox_path {
55 0     0 1   my $path;
56 0 0         if ( $^O eq 'MSWin32' ) {
    0          
57 0           $path = _firefox_windows_path();
58             }
59             elsif ( $^O eq 'darwin' ) {
60 0           $path = _firefox_darwin_path();
61             }
62             else {
63 0           $path = _firefox_unix_path;
64             }
65              
66 0 0         if ( not -x $path ) {
67 0           die $path . ' is not an executable file.';
68             }
69              
70 0           return $path;
71             }
72              
73              
74             # We want the profile to persist to the end of the session, not just
75             # the end of this function.
76             my $profile;
77              
78             sub setup_firefox_binary_env {
79 0     0 1   my ( $port, $marionette_port, $caller_profile ) = @_;
80              
81 0   0       $profile = $caller_profile || Selenium::Firefox::Profile->new;
82 0           $profile->add_webdriver( $port, $marionette_port );
83 0           $profile->add_marionette($marionette_port);
84              
85             # For non-geckodriver/marionette startup, we instruct Firefox to
86             # use the profile by specifying the appropriate environment
87             # variables for it to hook onto.
88 0 0         if ( !$marionette_port ) {
89 0           $ENV{'XRE_PROFILE_PATH'} = $profile->_layout_on_disk;
90 0           $ENV{'MOZ_NO_REMOTE'} = '1'; # able to launch multiple instances
91 0           $ENV{'MOZ_CRASHREPORTER_DISABLE'} = '1'; # disable breakpad
92 0           $ENV{'NO_EM_RESTART'} =
93             '1'; # prevent the binary from detaching from the console.log
94             }
95             else {
96             # In case the user created an old Firefox, which would've set
97             # those ENV variables, and then wanted to create a new Firefox
98             # afterwards, the env variables would still be around, and the
99             # new Firefox would respect the XRE_PROFILE_PATH and try to
100             # load it in the new geckodriver Firefox, which would cause an
101             # extension compatibility check
102 0           my @env_vars = qw/
103             XRE_PROFILE_PATH
104             MOZ_NO_REMOTE
105             MOZ_CRASHREPORTER_DISABLE
106             NO_EM_RESTART
107             /;
108              
109 0           foreach (@env_vars) {
110 0           delete $ENV{$_};
111             }
112             }
113              
114 0           return $profile;
115             }
116              
117             1;
118              
119             __END__