Explorar el Código

Refactor building binary port into its own library

Daniel Gempesaw hace 10 años
padre
commit
501b7fe648

+ 36 - 34
lib/Selenium/CanStartBinary.pm

@@ -1,7 +1,7 @@
 package Selenium::CanStartBinary;
 
 # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
-use IO::Socket::INET;
+use Selenium::CanStartBinary::ProbePort qw/find_open_port_above probe_port/;
 use Selenium::Waiter qw/wait_until/;
 use Selenium::Firefox::Binary qw/firefox_path setup_firefox_binary_env/;
 use Selenium::Firefox::Profile;
@@ -66,6 +66,32 @@ until we find a valid one.
 
 requires 'binary_port';
 
+=attr port
+
+The role will attempt to determine the proper port for us. Consuming
+roles should set a default port in L</binary_port> at which we will
+begin searching for an open port.
+
+Note that if we cannot locate a suitable L</binary>, port will be set
+to 4444 so we can attempt to look for a Selenium server at
+C<127.0.0.1:4444>.
+
+=cut
+
+has 'port' => (
+    is => 'lazy',
+    builder => sub {
+        my ($self) = @_;
+
+        if ($self->binary) {
+            return find_open_port_above($self->binary_port);
+        }
+        else {
+            return '4444'
+        }
+    }
+);
+
 has 'binary_mode' => (
     is => 'lazy',
     init_arg => undef,
@@ -113,27 +139,11 @@ sub BUILDARGS {
 sub _build_binary_mode {
     my ($self) = @_;
 
-    my $port = $self->start_binary_on_port;
-    $self->port($port);
-    return 1;
-}
-
-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 ($self) = @_;
-
     my $executable = $self->binary;
-    my $port = _find_open_port_above($self->binary_port);
+    return unless $executable;
 
+    my $port = $self->port;
+    return unless $port != 4444;
     if (ref($self) eq 'Selenium::Firefox') {
         setup_firefox_binary_env($port);
     }
@@ -142,7 +152,7 @@ sub start_binary_on_port {
     system($command);
     my $success = wait_until { probe_port($port) } timeout => 10;
     if ($success) {
-        return $port;
+        return 1;
     }
     else {
         die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
@@ -217,20 +227,12 @@ sub _command_suffix {
     }
 }
 
-sub _find_open_port_above {
-    my ($port) = @_;
+=head1 SEE ALSO
 
-    my $free_port = wait_until {
-        if ( probe_port($port) ) {
-            $port++;
-            return 0;
-        }
-        else {
-            return $port;
-        }
-    };
+Selenium::Chrome
+Selenium::Firefox
+Selenium::PhantomJS
 
-    return $free_port;
-}
+=cut
 
 1;

+ 34 - 0
lib/Selenium/CanStartBinary/ProbePort.pm

@@ -0,0 +1,34 @@
+package Selenium::CanStartBinary::ProbePort;
+
+use IO::Socket::INET;
+use Selenium::Waiter qw/wait_until/;
+
+require Exporter;
+our @ISA = qw/Exporter/;
+our @EXPORT_OK = qw/find_open_port_above probe_port/;
+
+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
+    );
+}

+ 0 - 6
lib/Selenium/Chrome.pm

@@ -35,12 +35,6 @@ has '+browser_name' => (
     default => sub { 'chrome' }
 );
 
-# By shadowing the parent's port function, we can set the port in
-# _build_binary_mode's builder
-has '+port' => (
-    is => 'lazy'
-);
-
 has 'binary' => (
     is => 'lazy',
     coerce => \&coerce_simple_binary,

+ 0 - 6
lib/Selenium/Firefox.pm

@@ -35,12 +35,6 @@ has '+browser_name' => (
     default => sub { 'firefox' }
 );
 
-# By shadowing the parent's port, we can set it in _build_binary_mode properly
-has '+port' => (
-    is => 'lazy',
-    default => sub { 4444 }
-);
-
 has 'binary' => (
     is => 'lazy',
     coerce => \&coerce_firefox_binary,

+ 0 - 6
lib/Selenium/PhantomJS.pm

@@ -36,12 +36,6 @@ has '+browser_name' => (
     default => sub { 'phantomjs' }
 );
 
-# By shadowing the parent's port function, we can set the port in
-# _build_binary_mode's builder
-has '+port' => (
-    is => 'lazy'
-);
-
 has 'binary' => (
     is => 'lazy',
     coerce => \&coerce_simple_binary,