Selaa lähdekoodia

Refactor finding binaries into its own library

Daniel Gempesaw 10 vuotta sitten
vanhempi
sitoutus
ac6e724822

+ 1 - 30
lib/Selenium/CanStartBinary.pm

@@ -1,8 +1,6 @@
 package Selenium::CanStartBinary;
 
 # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
-use Cwd qw/abs_path/;
-use File::Which qw/which/;
 use IO::Socket::INET;
 use Selenium::Waiter qw/wait_until/;
 use Selenium::Firefox::Binary qw/firefox_path setup_firefox_binary_env/;
@@ -107,7 +105,6 @@ sub BUILDARGS {
         # Windows may throw a fit about invalid pointers if we try to
         # connect to localhost instead of 127.1
         $args{remote_server_addr} = '127.0.0.1';
-
     }
 
     return { %args };
@@ -134,7 +131,7 @@ sub probe_port {
 sub start_binary_on_port {
     my ($self) = @_;
 
-    my $executable = $self->_find_executable;
+    my $executable = $self->binary;
     my $port = _find_open_port_above($self->binary_port);
 
     if (ref($self) eq 'Selenium::Firefox') {
@@ -163,34 +160,8 @@ sub shutdown_binary {
     }
 }
 
-sub _find_executable {
     my ($self) = @_;
 
-    # If the user specified the full path to the binary, we don't have
-    # any work to do.
-    if ($self->has_binary) {
-        if (-x abs_path($self->binary)) {
-            return abs_path($self->binary);
-        }
-        else {
-            die 'The binary at ' . $self->binary . ' is not executable. Choose the correct file or chmod +x it as needed.';
-        }
-    }
-
-    my $binary = $self->binary;
-    if ($binary eq 'firefox') {
-        return firefox_path();
-    }
-    else {
-        my $executable = which($binary);
-
-        if (not defined $executable) {
-            warn qq(Unable to find the $binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
-        }
-        else {
-            return $executable;
-        }
-    }
 }
 
 sub _construct_command {

+ 61 - 0
lib/Selenium/CanStartBinary/FindBinary.pm

@@ -0,0 +1,61 @@
+package Selenium::CanStartBinary::FindBinary;
+
+use File::Which qw/which/;
+use Cwd qw/abs_path/;
+use File::Which qw/which/;
+use IO::Socket::INET;
+use Selenium::Firefox::Binary qw/firefox_path/;
+
+require Exporter;
+our @ISA = qw/Exporter/;
+our @EXPORT_OK = qw/coerce_simple_binary coerce_firefox_binary/;
+
+sub coerce_simple_binary {
+    my ($executable) = @_;
+
+    my $manual_binary = _validate_manual_binary($executable);
+    if ($manual_binary) {
+        return $manual_binary;
+    }
+    else {
+        return _naive_find_binary($executable);
+    }
+}
+
+sub coerce_firefox_binary {
+    my ($executable) = @_;
+
+    my $manual_binary = _validate_manual_binary($executable);
+    if ($manual_binary) {
+        return $manual_binary;
+    }
+    else {
+        return firefox_path();
+    }
+}
+
+sub _validate_manual_binary {
+    my ($executable) = @_;
+
+    my $abs_executable = eval { abs_path($executable) };
+    if ( $abs_executable ) {
+        if ( -x $abs_executable ) {
+            return $abs_executable;
+        }
+        else {
+            die 'The binary at ' . $executable . ' is not executable. Choose the correct file or chmod +x it as needed.';
+        }
+    }
+}
+
+sub _naive_find_binary {
+    my ($executable) = @_;
+    my $naive_binary = which($executable);
+    if (defined $naive_binary) {
+        return $naive_binary;
+    }
+    else {
+        warn qq(Unable to find the $naive_binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
+        return;
+    }
+}

+ 2 - 0
lib/Selenium/Chrome.pm

@@ -2,6 +2,7 @@ package Selenium::Chrome;
 
 # ABSTRACT: A convenience package for creating a Chrome instance
 use Moo;
+use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary/;
 extends 'Selenium::Remote::Driver';
 
 =head1 SYNOPSIS
@@ -42,6 +43,7 @@ has '+port' => (
 
 has 'binary' => (
     is => 'lazy',
+    coerce => \&coerce_simple_binary,
     default => sub { 'chromedriver' },
     predicate => 1
 );

+ 2 - 0
lib/Selenium/Firefox.pm

@@ -2,6 +2,7 @@ package Selenium::Firefox;
 
 # ABSTRACT: A convenience package for creating a Firefox instance
 use Moo;
+use Selenium::CanStartBinary::FindBinary qw/coerce_firefox_binary/;
 extends 'Selenium::Remote::Driver';
 
 =head1 SYNOPSIS
@@ -42,6 +43,7 @@ has '+port' => (
 
 has 'binary' => (
     is => 'lazy',
+    coerce => \&coerce_firefox_binary,
     default => sub { 'firefox' },
     predicate => 1
 );

+ 2 - 0
lib/Selenium/PhantomJS.pm

@@ -2,6 +2,7 @@ package Selenium::PhantomJS;
 
 # ABSTRACT: A convenience package for creating a PhantomJS instance
 use Moo;
+use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary/;
 extends 'Selenium::Remote::Driver';
 
 =head1 SYNOPSIS
@@ -43,6 +44,7 @@ has '+port' => (
 
 has 'binary' => (
     is => 'lazy',
+    coerce => \&coerce_simple_binary,
     default => sub { 'phantomjs' },
     predicate => 1
 );