diff --git a/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java index d4a6441b..2674cc08 100644 --- a/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java +++ b/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java @@ -223,6 +223,11 @@ public class FrameImpl extends ChannelOwner implements Frame { return null; } + + private static String toProtocol(NavigateOptions.WaitUntil waitUntil) { + return waitUntil.toString().toLowerCase(); + } + @Override public ResponseImpl navigate(String url, NavigateOptions options) { if (options == null) { @@ -230,6 +235,10 @@ public class FrameImpl extends ChannelOwner implements Frame { } JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); params.addProperty("url", url); + if (options.waitUntil != null) { + params.remove("waitUntil"); + params.addProperty("waitUntil", toProtocol(options.waitUntil)); + } JsonElement result = sendMessage("goto", params); return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("response").get("guid").getAsString()); } diff --git a/lib/src/test/java/com/microsoft/playwright/Server.java b/lib/src/test/java/com/microsoft/playwright/Server.java index 0af625f8..49e1807f 100644 --- a/lib/src/test/java/com/microsoft/playwright/Server.java +++ b/lib/src/test/java/com/microsoft/playwright/Server.java @@ -43,6 +43,7 @@ public class Server implements HttpHandler { private final Map> requestSubscribers = Collections.synchronizedMap(new HashMap<>()); private final Map auths = Collections.synchronizedMap(new HashMap<>()); + private final Map routes = Collections.synchronizedMap(new HashMap<>()); private static class Auth { public final String user; @@ -95,6 +96,11 @@ public class Server implements HttpHandler { return future; } + + void setRoute(String path, HttpHandler handler) { + routes.put(path, handler); + } + @Override public void handle(HttpExchange exchange) throws IOException { String path = exchange.getRequestURI().getPath(); @@ -123,6 +129,20 @@ public class Server implements HttpHandler { } } + synchronized (requestSubscribers) { + CompletableFuture subscriber = requestSubscribers.get(path); + if (subscriber != null) { + requestSubscribers.remove(path); + subscriber.complete(new Request(exchange.getRequestHeaders())); + } + } + + HttpHandler handler = routes.get(path); + if (handler != null) { + handler.handle(exchange); + return; + } + File file = new File(resourcesDir, path.substring(1)); exchange.getResponseHeaders().put("Content-Type", singletonList(mimeType(file))); try (FileInputStream input = new FileInputStream(file)) { @@ -135,14 +155,6 @@ public class Server implements HttpHandler { } } exchange.getResponseBody().close(); - - synchronized (requestSubscribers) { - CompletableFuture subscriber = requestSubscribers.get(path); - if (subscriber != null) { - requestSubscribers.remove(path); - subscriber.complete(new Request(exchange.getRequestHeaders())); - } - } } private static void copy(InputStream in, OutputStream out) throws IOException { diff --git a/lib/src/test/java/com/microsoft/playwright/TestFrameNavigate.java b/lib/src/test/java/com/microsoft/playwright/TestFrameNavigate.java index 2f25b8ac..16b2dad5 100644 --- a/lib/src/test/java/com/microsoft/playwright/TestFrameNavigate.java +++ b/lib/src/test/java/com/microsoft/playwright/TestFrameNavigate.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.*; import java.io.IOException; +import static com.microsoft.playwright.Page.NavigateOptions.WaitUntil.NETWORKIDLE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -62,7 +63,7 @@ public class TestFrameNavigate { } @Test - void should_navigate_subframes() { + void shouldNavigateSubframes() { page.navigate(server.PREFIX + "/frames/one-frame.html"); assertTrue(page.frames().get(0).url().contains("/frames/one-frame.html")); assertTrue(page.frames().get(1).url().contains("/frames/frame.html")); @@ -71,4 +72,24 @@ public class TestFrameNavigate { assertTrue(response.ok()); assertEquals(page.frames().get(1), response.frame()); } + + // TODO: not supported in sync api + void shouldRejectWhenFrameDetaches() { + } + + @Test + void shouldContinueAfterClientRedirect() { + server.setRoute("/frames/script.js", (httpExchange) -> {}); + String url = server.PREFIX + "/frames/child-redirect.html"; + try { + page.navigate(url, new Page.NavigateOptions().withTimeout(5000).withWaitUntil(NETWORKIDLE)); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("Timeout 5000ms exceeded.")); + assertTrue(e.getMessage().contains("navigating to \"" + url +"\", waiting until \"networkidle\"")); + } + } + + // TODO: not supported in sync api + void shouldReturnMatchingResponses() { + } }