Эх сурвалжийг харах

Fix #219: Add startup timeout attribute to CanStartBinary

Daniel Gempesaw 10 жил өмнө
parent
commit
1cae30083b

+ 27 - 1
lib/Selenium/CanStartBinary.pm

@@ -121,6 +121,32 @@ has '+port' => (
     }
 );
 
+=attr startup_timeout
+
+Optional: you can modify how long we will wait for the binary to start
+up. By default, we will start the binary and check the intended
+destination port for 10 seconds before giving up. If the machine
+you're using to run your browsers is slower or smaller, you may need
+to increase this timeout.
+
+The following:
+
+    my $f = Selenium::Firefox->new(
+        startup_timeout => 60
+    );
+
+will wait up to 60 seconds for the firefox binary to respond on the
+proper port. To use this constructor option, you should specify a time
+in seconds as an integer, and it will be passed to the arguments
+section of a L<Selenium::Waiter/wait_until> subroutine call.
+
+=cut
+
+has startup_timeout => (
+    is => 'lazy',
+    default => sub { 10 }
+);
+
 =attr binary_mode
 
 Mostly intended for internal use, its builder coordinates all the side
@@ -228,7 +254,7 @@ sub _build_binary_mode {
     my $command = $self->_construct_command;
     system($command);
 
-    my $success = wait_until { probe_port($port) } timeout => 10;
+    my $success = wait_until { probe_port($port) } timeout => $self->startup_timeout;
     if ($success) {
         return 1;
     }

+ 24 - 0
t/CanStartBinary.t

@@ -7,6 +7,7 @@ use Selenium::Chrome;
 use Selenium::Firefox;
 use Selenium::Firefox::Binary;
 use Selenium::PhantomJS;
+use Sub::Install;
 use Test::Fatal;
 use Test::More;
 
@@ -78,6 +79,29 @@ FIREFOX: {
     }
 }
 
+TIMEOUT: {
+    my $binary = Selenium::Firefox::Binary::firefox_path();
+    skip 'Firefox binary not found in path', 3
+      unless $binary;
+
+    # Force the port check to exhaust the wait_until timeout.
+    Sub::Install::reinstall_sub({
+        code => sub { 0 },
+        into => 'Selenium::CanStartBinary',
+        as => 'probe_port'
+    });
+
+    my $start = time;
+    eval { Selenium::Firefox->new( startup_timeout => 1 ) };
+    # The test leaves a bit of a cushion to handle any unexpected
+    # latency issues when starting up the browser - the important part
+    # is that our timeout duration is _not_ the default 10 seconds.
+    ok( time - $start < 5, 'We can specify how long to wait for a binary to be available'  );
+
+}
+
+
+
 sub is_proper_phantomjs_available {
     my $ver = `phantomjs --version` // '';
     chomp $ver;