Selaa lähdekoodia

Combine Selenium::Binary into binary builder role

Daniel Gempesaw 10 vuotta sitten
vanhempi
sitoutus
e788df335f
4 muutettua tiedostoa jossa 99 lisäystä ja 114 poistoa
  1. 0 105
      lib/Selenium/Binary.pm
  2. 94 2
      lib/Selenium/BinaryModeBuilder.pm
  3. 1 1
      lib/Selenium/Firefox/Binary.pm
  4. 4 6
      t/binary.t

+ 0 - 105
lib/Selenium/Binary.pm

@@ -1,105 +0,0 @@
-package Selenium::Binary;
-
-# ABSTRACT: Execute a binary asynchronously on a specific port
-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/;
-use Selenium::Firefox::Profile;
-
-require Exporter;
-our @ISA = qw/Exporter/;
-our @EXPORT = qw/start_binary_on_port/;
-our @EXPORT_OK = qw/_find_open_port_above _probe_port/;
-
-sub start_binary_on_port {
-    my ($process, $port) = @_;
-
-    my $executable = _find_executable($process);
-    $port = _find_open_port_above($port);
-    if ($process eq 'firefox') {
-        setup_firefox_binary_env($port);
-    }
-    my $command = _construct_command($executable, $port);
-
-    system($command);
-    my $success = wait_until { _probe_port($port) } timeout => 10;
-    if ($success) {
-        return $port;
-    }
-    else {
-        die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
-    }
-}
-
-sub _find_executable {
-    my ($binary) = @_;
-
-    if ($binary eq 'firefox') {
-        return firefox_path();
-    }
-    else {
-        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);
-        }
-        else {
-            return $executable;
-        }
-    }
-}
-
-sub _construct_command {
-    my ($executable, $port) = @_;
-
-    my %args;
-    if ($executable =~ /chromedriver$/) {
-        %args = (
-            port => $port,
-            'base-url' => 'wd/hub'
-        );
-    }
-    elsif ($executable =~ /phantomjs$/) {
-        %args = (
-            webdriver => '127.0.0.1:' . $port
-        );
-    }
-    elsif ($executable =~ /firefox/i) {
-        $executable .= ' -no-remote ';
-    }
-
-    my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
-
-    return join(' ', ($executable, @args, '> /dev/null 2>&1 &') );
-}
-
-sub _find_open_port_above {
-    my ($port) = @_;
-
-    my $free_port = wait_until {
-        if (_probe_port($port)) {
-            $port++;
-            return 0;
-        }
-        else {
-            return $port;
-        }
-    };
-
-    return $free_port;
-}
-
-sub _probe_port {
-    my ($port) = @_;
-
-    return IO::Socket::INET->new(
-        PeerAddr => '127.0.0.1',
-        PeerPort => $port,
-        Timeout => 3
-    );
-}
-
-
-
-1;

+ 94 - 2
lib/Selenium/BinaryModeBuilder.pm

@@ -1,7 +1,11 @@
 package Selenium::BinaryModeBuilder;
 
-# ABSTRACT: Role to teach a class how to enable its binary
-use Selenium::Binary qw/start_binary_on_port/;
+# ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
+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/;
+use Selenium::Firefox::Profile;
 use Try::Tiny;
 use Moo::Role;
 
@@ -30,4 +34,92 @@ sub _build_binary_mode {
     }
 }
 
+sub probe_port {
+    my ($port) = @_;
+
+    return IO::Socket::INET->new(
+        PeerAddr => '127.0.0.1',
+        PeerPort => $port,
+        Timeout => 3
+    );
+}
+
+sub start_binary_on_port {
+    my ($process, $port) = @_;
+
+    my $executable = _find_executable($process);
+    $port = _find_open_port_above($port);
+    if ($process eq 'firefox') {
+        setup_firefox_binary_env($port);
+    }
+    my $command = _construct_command($executable, $port);
+
+    system($command);
+    my $success = wait_until { probe_port($port) } timeout => 10;
+    if ($success) {
+        return $port;
+    }
+    else {
+        die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
+    }
+}
+
+sub _find_executable {
+    my ($binary) = @_;
+
+    if ($binary eq 'firefox') {
+        return firefox_path();
+    }
+    else {
+        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);
+        }
+        else {
+            return $executable;
+        }
+    }
+}
+
+sub _construct_command {
+    my ($executable, $port) = @_;
+
+    my %args;
+    if ($executable =~ /chromedriver$/) {
+        %args = (
+            port => $port,
+            'base-url' => 'wd/hub'
+        );
+    }
+    elsif ($executable =~ /phantomjs$/) {
+        %args = (
+            webdriver => '127.0.0.1:' . $port
+        );
+    }
+    elsif ($executable =~ /firefox/i) {
+        $executable .= ' -no-remote ';
+    }
+
+    my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
+
+    return join(' ', ($executable, @args, '> /dev/null 2>&1 &') );
+}
+
+sub _find_open_port_above {
+    my ($port) = @_;
+
+    my $free_port = wait_until {
+        if ( probe_port($port) ) {
+            $port++;
+            return 0;
+        }
+        else {
+            return $port;
+        }
+    };
+
+    return $free_port;
+}
+
 1;

+ 1 - 1
lib/Selenium/Firefox/Binary.pm

@@ -16,7 +16,7 @@ sub _firefox_windows_path {
 sub _firefox_darwin_path {
     my $default_firefox = '/Applications/Firefox.app/Contents/MacOS/firefox-bin';
 
-    if (-e $default_firefox) {
+    if (-e $default_firefox && -x $default_firefox) {
         return $default_firefox
     }
     else {

+ 4 - 6
t/binary.t

@@ -2,7 +2,6 @@
 
 use strict;
 use warnings;
-use Selenium::Binary qw/_probe_port/;
 use Selenium::Firefox::Binary;
 use Selenium::Chrome;
 use Selenium::PhantomJS;
@@ -24,7 +23,7 @@ PHANTOMJS: {
     is( $phantom->browser_name, 'phantomjs', 'binary phantomjs is okay');
     isnt( $phantom->port, 4444, 'phantomjs can start up its own binary');
 
-    ok( _probe_port( $phantom->port ), 'the phantomjs binary is listening on its port');
+    ok( Selenium::BinaryModeBuilder::probe_port( $phantom->port ), 'the phantomjs binary is listening on its port');
 }
 
 CHROME: {
@@ -33,20 +32,19 @@ CHROME: {
     ok( $chrome->browser_name eq 'chrome', 'convenience chrome is okay' );
     isnt( $chrome->port, 4444, 'chrome can start up its own binary');
 
-    ok( _probe_port( $chrome->port ), 'the chrome binary is listening on its port');
+    ok( Selenium::BinaryModeBuilder::probe_port( $chrome->port ), 'the chrome binary is listening on its port');
 }
 
 FIREFOX: {
     my $binary = Selenium::Firefox::Binary::firefox_path();
     ok(-x $binary, 'we can find some sort of firefox');
 
-    my $command = Selenium::Binary::_construct_command('firefox', 1234);
+    my $command = Selenium::BinaryModeBuilder::_construct_command('firefox', 1234);
     ok($command =~ /firefox -no-remote/, 'firefox command has proper args');
 
     my $firefox = Selenium::Firefox->new;
     isnt( $firefox->port, 4444, 'firefox can start up its own binary');
-
-    ok( _probe_port( $firefox->port ), 'the firefox binary is listening on its port');
+    ok( Selenium::BinaryModeBuilder::probe_port( $firefox->port ), 'the firefox binary is listening on its port');
 }
 
 done_testing;