diff --git a/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java b/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java index b2b929a720a2f..38d358d001c3e 100644 --- a/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java +++ b/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java @@ -17,10 +17,13 @@ package org.openqa.selenium.firefox; +import static java.util.concurrent.TimeUnit.SECONDS; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.net.PortProber; import org.openqa.selenium.firefox.internal.Executable; import org.openqa.selenium.remote.service.DriverService; @@ -66,7 +69,7 @@ public static GeckoDriverService createDefaultService() { @Override protected void waitUntilAvailable() throws MalformedURLException { - return; + PortProber.waitForPortUp(getUrl().getPort(), 20, SECONDS); } /** diff --git a/java/client/src/org/openqa/selenium/net/PortProber.java b/java/client/src/org/openqa/selenium/net/PortProber.java index c48081f3d35b9..8a46152ef8ce0 100644 --- a/java/client/src/org/openqa/selenium/net/PortProber.java +++ b/java/client/src/org/openqa/selenium/net/PortProber.java @@ -25,6 +25,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; +import java.net.SocketTimeoutException; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -156,4 +157,22 @@ public static boolean pollPort(int port, int timeout, TimeUnit unit) { return false; } + + public static void waitForPortUp(int port, int timeout, TimeUnit unit) { + long end = System.currentTimeMillis() + unit.toMillis(timeout); + while (System.currentTimeMillis() < end) { + try { + Socket socket = new Socket(); + socket.connect(new InetSocketAddress("localhost", port), 1000); + socket.close(); + return; + } catch (ConnectException e) { + // Ignore this + } catch (SocketTimeoutException e) { + // Ignore this + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } }