LocalWebDriverBiDi: make the command timeout configurable

This commit is contained in:
Alex Osborne committed 2026-11-12 13:50:20 +09:00
1 parent 9cefc32df0
commit 0f8644fd65
2 files changed
+16 -3

No files matched your search

@@ -32,6 +32,7 @@ import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.nio.file.Path;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
@@ -58,6 +59,7 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable {
private final Thread shutdownHook;
private final Object sendLock = new Object();
private CompletableFuture<?> sendChain = CompletableFuture.completedFuture(null);
private volatile Duration commandTimeout = Duration.ofSeconds(30);
private static final int LAUNCH_TIMEOUT_SECONDS = 60;
private static final String[] BROWSERS = new String[]{
"firefox", "/Applications/Firefox.app/Contents/MacOS/firefox", "chromedriver"};
@@ -175,6 +177,14 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable {
return future;
}
/**
* How long to wait for the reply to a command before throwing {@link WebDriverTimeoutException}.
* Commands that return a {@link CompletableFuture} aren't affected.
*/
public void setCommandTimeout(Duration commandTimeout) {
this.commandTimeout = commandTimeout;
}
@Override
public boolean isAlive() {
return process.isAlive() && !webSocket.isInputClosed() && !webSocket.isOutputClosed();
@@ -247,9 +257,10 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable {
parameterizedType.getActualTypeArguments()[0] instanceof Class<?> typeClass) {
yield future.thenApply(result -> BiDiJson.fromJson(result, typeClass));
} else {
int timeout = 30;
Duration timeout = commandTimeout;
try {
yield BiDiJson.fromJson(future.get(timeout, TimeUnit.SECONDS), method.getReturnType());
yield BiDiJson.fromJson(future.get(timeout.toMillis(), TimeUnit.MILLISECONDS),
method.getReturnType());
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException re) throw re;
throw e;
@@ -258,7 +269,7 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable {
logger.log(System.Logger.Level.WARNING, "Browser stopped responding, killing it");
kill();
}
throw new WebDriverTimeoutException(prefix + methodName + " took more than " + timeout + " seconds");
throw new WebDriverTimeoutException(prefix + methodName + " took more than " + timeout.toSeconds() + " seconds");
}
}
}
@@ -24,6 +24,7 @@ import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.*;
import java.util.zip.GZIPOutputStream;
@@ -129,6 +130,7 @@ class BrowserProcessorTest {
.findFirst().orElseThrow();
processor.setBehaviors(List.of(page -> {
if (browserProcess.isAlive()) {
processor.pool.get(0).driver.setCommandTimeout(Duration.ofSeconds(2));
suspend(browserProcess);
page.eval("() => 1");
}