1
0
mirror of synced 2026-08-05 15:06:54 +00:00

feat: support waitUntil in frame.navigate

This commit is contained in:
Yury Semikhatsky
2020-10-06 10:21:48 -07:00
parent c902cb44ca
commit 5f03ff01c5
3 changed files with 51 additions and 9 deletions
@@ -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());
}
@@ -43,6 +43,7 @@ public class Server implements HttpHandler {
private final Map<String, CompletableFuture<Request>> requestSubscribers = Collections.synchronizedMap(new HashMap<>());
private final Map<String, Auth> auths = Collections.synchronizedMap(new HashMap<>());
private final Map<String, HttpHandler> 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<Request> 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<Request> 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 {
@@ -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() {
}
}