Browse Source

Add binary_path to allow users to specify the binary

Daniel Gempesaw 10 years ago
parent
commit
35daab1b25
2 changed files with 47 additions and 14 deletions
  1. 19 14
      lib/Selenium/CanStartBinary.pm
  2. 28 0
      t/CanStartBinary.t

+ 19 - 14
lib/Selenium/CanStartBinary.pm

@@ -6,7 +6,6 @@ use IO::Socket::INET;
 use Selenium::Waiter qw/wait_until/;
 use Selenium::Firefox::Binary qw/firefox_path setup_firefox_binary_env/;
 use Selenium::Firefox::Profile;
-use Try::Tiny;
 use Moo::Role;
 
 has 'binary_mode' => (
@@ -43,15 +42,12 @@ sub BUILDARGS {
 sub _build_binary_mode {
     my ($self) = @_;
 
-    return try {
-        my $port = start_binary_on_port($self->binary_name, $self->binary_port);
-        $self->port($port);
-        return 1;
-    }
-    catch {
-        warn $_;
-        return 0;
-    };
+    my $port = $self->start_binary_on_port(
+        $self->binary_name,
+        $self->binary_port
+    );
+    $self->port($port);
+    return 1;
 }
 
 sub probe_port {
@@ -65,9 +61,9 @@ sub probe_port {
 }
 
 sub start_binary_on_port {
-    my ($process, $port) = @_;
+    my ($self, $process, $port) = @_;
 
-    my $executable = _find_executable($process);
+    my $executable = $self->_find_executable($process);
     $port = _find_open_port_above($port);
     if ($process eq 'firefox') {
         setup_firefox_binary_env($port);
@@ -96,7 +92,16 @@ sub shutdown_binary {
 }
 
 sub _find_executable {
-    my ($binary) = @_;
+    my ($self, $binary) = @_;
+
+    if ($self->has_binary_path) {
+        if (-x $self->binary_path) {
+            return $self->binary_path;
+        }
+        else {
+            die 'The binary at "' . $self->binary_path . '" is not executable. Fix the path or chmod +x it as needed.';
+        }
+    }
 
     if ($binary eq 'firefox') {
         return firefox_path();
@@ -105,7 +110,7 @@ sub _find_executable {
         my $executable = which($binary);
 
         if (not defined $executable) {
-            die qq(Unable to find the $binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
+            warn qq(Unable to find the $binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
         }
         else {
             return $executable;

+ 28 - 0
t/CanStartBinary.t

@@ -7,6 +7,7 @@ use Selenium::Chrome;
 use Selenium::Firefox;
 use Selenium::Firefox::Binary;
 use Selenium::PhantomJS;
+use Test::Fatal;
 use Test::More;
 
 unless ( $ENV{RELEASE_TESTING} ) {
@@ -30,6 +31,33 @@ PHANTOMJS: {
     }
 }
 
+MANUAL: {
+    package ManualChrome {
+        use Moo;
+        with 'Selenium::CanStartBinary';
+        has 'binary_name' => ( is => 'ro', default => 'fake' );
+        has 'binary_port' => ( is => 'ro', default => 12345 );
+        1
+    };
+
+    ok( exception { ManualChrome->new( binary_path => '/not/executable') },
+        'we throw if the user specified binary is not executable');
+
+  SKIP: {
+        my $phantom_binary = which('phantomjs');
+        skip 'PhantomJS needed for manual binary path tests', 3
+          unless $phantom_binary;
+
+        my $manual_phantom = Selenium::PhantomJS->new(
+            binary_path => $phantom_binary
+        );
+        ok( $manual_phantom->browser_name eq 'phantomjs', 'convenience phantom is okay' );
+        isnt( $manual_phantom->port, 4444, 'phantom can start up its own binary');
+
+        ok( Selenium::CanStartBinary::probe_port( $manual_phantom->port ), 'the chrome binary is listening on its port');
+    }
+}
+
 CHROME: {
   SKIP: {
         my $has_chromedriver = which('chromedriver');