diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java index c91f0e8c..d68f871c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java @@ -457,14 +457,27 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { private void unroute(UrlMatcher matcher, Consumer handler) { withLogging("BrowserContext.unroute", () -> { routes.remove(matcher, handler); - if (routes.size() == 0) { - JsonObject params = new JsonObject(); - params.addProperty("enabled", false); - sendMessage("setNetworkInterceptionEnabled", params); - } + maybeDisableNetworkInterception(); }); } + private void maybeDisableNetworkInterception() { + if (routes.size() == 0) { + JsonObject params = new JsonObject(); + params.addProperty("enabled", false); + sendMessage("setNetworkInterceptionEnabled", params); + } + } + + void handleRoute(Route route) { + boolean handled = routes.handle(route); + if (handled) { + maybeDisableNetworkInterception(); + } else { + route.resume(); + } + } + void pause() { sendMessage("pause"); } @@ -473,10 +486,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { protected void handleEvent(String event, JsonObject params) { if ("route".equals(event)) { Route route = connection.getExistingObject(params.getAsJsonObject("route").get("guid").getAsString()); - boolean handled = routes.handle(route); - if (!handled) { - route.resume(); - } + handleRoute(route); } else if ("page".equals(event)) { PageImpl page = connection.getExistingObject(params.getAsJsonObject("page").get("guid").getAsString()); pages.add(page); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java index bb364110..d4b63476 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -198,11 +198,10 @@ public class PageImpl extends ChannelOwner implements Page { } else if ("route".equals(event)) { Route route = connection.getExistingObject(params.getAsJsonObject("route").get("guid").getAsString()); boolean handled = routes.handle(route); - if (!handled) { - handled = browserContext.routes.handle(route); - } - if (!handled) { - route.resume(); + if (handled) { + maybeDisableNetworkInterception(); + } else { + browserContext.handleRoute(route); } } else if ("video".equals(event)) { String artifactGuid = params.getAsJsonObject("artifact").get("guid").getAsString(); @@ -1181,14 +1180,18 @@ public class PageImpl extends ChannelOwner implements Page { private void unroute(UrlMatcher matcher, Consumer handler) { withLogging("Page.unroute", () -> { routes.remove(matcher, handler); - if (routes.size() == 0) { - JsonObject params = new JsonObject(); - params.addProperty("enabled", false); - sendMessage("setNetworkInterceptionEnabled", params); - } + maybeDisableNetworkInterception(); }); } + private void maybeDisableNetworkInterception() { + if (routes.size() == 0) { + JsonObject params = new JsonObject(); + params.addProperty("enabled", false); + sendMessage("setNetworkInterceptionEnabled", params); + } + } + @Override public String url() { return mainFrame.url();