Переглянути джерело

Option to probe only for the given port without probing for free ports above it

Vangelis Katsikaros 9 роки тому
батько
коміт
fac908d781

+ 37 - 3
lib/Selenium/CanStartBinary.pm

@@ -2,7 +2,7 @@ package Selenium::CanStartBinary;
 
 # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
 use File::Spec;
-use Selenium::CanStartBinary::ProbePort qw/find_open_port_above probe_port/;
+use Selenium::CanStartBinary::ProbePort qw/find_open_port_above find_open_port probe_port/;
 use Selenium::Firefox::Binary qw/setup_firefox_binary_env/;
 use Selenium::Waiter qw/wait_until/;
 use Moo::Role;
@@ -136,7 +136,12 @@ has '+port' => (
         my ($self) = @_;
 
         if ($self->_real_binary) {
-            return find_open_port_above($self->binary_port);
+            if ($self->fixed_ports) {
+                return find_open_port($self->binary_port);
+			}
+            else {
+                return find_open_port_above($self->binary_port);
+            }
         }
         else {
             return 4444
@@ -144,6 +149,30 @@ has '+port' => (
     }
 );
 
+=attr fixed_ports
+
+Optional: By default, if binary_port and marionette_port are not free
+a higher free port is probed and acquired if possible, until a free one
+if found or a timeout is exceeded.
+
+    my $driver1 = Selenium::Chrome->new;
+    my $driver2 = Selenium::Chrome->new( port => 1234 );
+
+The default behavior can be overridden. In this case, only the default
+or given binary_port and marionette_port are probed, without probing 
+higher ports. This ensures that either the default or given port will be
+assigned, or no port will be assigned at all.
+
+    my $driver1 = Selenium::Chrome->new( fixed_ports => 1 );
+    my $driver2 = Selenium::Chrome->new( port => 1234, fixed_ports => 1);
+
+=cut
+
+has 'fixed_ports' => (
+    is => 'lazy',
+    default => sub { 0 }
+);
+
 =attr custom_args
 
 Optional: If you want to pass additional options to the binary when it
@@ -174,7 +203,12 @@ has 'marionette_port' => (
             return 0;
         }
         else {
-            return find_open_port_above($self->marionette_binary_port);
+            if ($self->fixed_ports) {
+                return find_open_port($self->marionette_binary_port);
+			}
+            else {
+                return find_open_port_above($self->marionette_binary_port);
+            }
         }
     }
 );

+ 7 - 1
lib/Selenium/CanStartBinary/ProbePort.pm

@@ -6,7 +6,7 @@ use Selenium::Waiter qw/wait_until/;
 
 require Exporter;
 our @ISA = qw/Exporter/;
-our @EXPORT_OK = qw/find_open_port_above probe_port/;
+our @EXPORT_OK = qw/find_open_port_above find_open_port probe_port/;
 
 sub find_open_port_above {
     my ($port) = @_;
@@ -24,6 +24,12 @@ sub find_open_port_above {
     return $free_port;
 }
 
+sub find_open_port {
+    my ($port) = @_;
+
+    probe_port($port) ? return 0 : return $port;
+}
+
 sub probe_port {
     my ($port) = @_;