From 44cb76d92ca4517e71e38732e0a71c3ed9eed27e Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 28 Jun 2022 16:23:04 -0700 Subject: [PATCH] chore: only log har when pw:api is enabled (#977) --- .../java/com/microsoft/playwright/impl/HARRouter.java | 9 +++++++-- .../com/microsoft/playwright/impl/LoggingSupport.java | 4 ++++ .../main/java/com/microsoft/playwright/impl/Router.java | 3 --- .../com/microsoft/playwright/TestBrowserContextHar.java | 3 +-- .../microsoft/playwright/TestPageRequestFallback.java | 4 ++++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/HARRouter.java b/playwright/src/main/java/com/microsoft/playwright/impl/HARRouter.java index 07b47835..ab0f241a 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/HARRouter.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/HARRouter.java @@ -26,6 +26,7 @@ import java.nio.file.Path; import java.util.Base64; import java.util.Map; +import static com.microsoft.playwright.impl.LoggingSupport.isApiLoggingEnabled; import static com.microsoft.playwright.impl.LoggingSupport.logApi; import static com.microsoft.playwright.impl.Serialization.fromNameValues; import static com.microsoft.playwright.impl.Serialization.gson; @@ -66,7 +67,9 @@ public class HARRouter { String action = response.get("action").getAsString(); if ("redirect".equals(action)) { String redirectURL = response.get("redirectURL").getAsString(); - logApi("HAR: " + route.request().url() + " redirected to " + redirectURL); + if (isApiLoggingEnabled()) { + logApi("HAR: " + route.request().url() + " redirected to " + redirectURL); + } ((RouteImpl) route).redirectNavigationRequest(redirectURL); return; } @@ -83,7 +86,9 @@ public class HARRouter { } if ("error".equals(action)) { - logApi("HAR: " + response.get("message").getAsString()); + if (isApiLoggingEnabled()) { + logApi("HAR: " + response.get("message").getAsString()); + } // Report the error, but fall through to the default handler. } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java b/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java index b2598a69..1348057a 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LoggingSupport.java @@ -60,6 +60,10 @@ class LoggingSupport { System.err.println(timestamp + " " + message); } + static boolean isApiLoggingEnabled() { + return isEnabled; + } + static void logApi(String message) { // This matches log format produced by the server. logWithTimestamp("pw:api " + message); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Router.java b/playwright/src/main/java/com/microsoft/playwright/impl/Router.java index 5b6b7c09..58240138 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Router.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Router.java @@ -39,9 +39,6 @@ class Router { } boolean handle(RouteImpl route) { - if (times != null && times <= 0) { - return false; - } if (!matcher.test(route.request().url())) { return false; } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextHar.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextHar.java index 72492bc0..305ed0fc 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextHar.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextHar.java @@ -326,8 +326,7 @@ public class TestBrowserContextHar extends TestBase { assertEquals("3", page2.evaluate(fetchFunction, "3")); assertEquals("3", page2.evaluate(fetchFunction, "3")); try { - Object result = page2.evaluate(fetchFunction, "4"); - System.out.println(result); + page2.evaluate(fetchFunction, "4"); fail("did not throw"); } catch (PlaywrightException e) { } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java index da608952..10ecad8b 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java @@ -133,10 +133,14 @@ public class TestPageRequestFallback extends TestBase { @Test void shouldChainOnce() { page.route("**/empty.html", route -> { + System.out.println("before fulfill"); route.fulfill(new Route.FulfillOptions().setStatus(200).setBody("fulfilled one")); + System.out.println("after fulfill"); }, new Page.RouteOptions().setTimes(1)); page.route("**/empty.html", route -> { + System.out.println("before fallback"); route.fallback(); + System.out.println("after fallback"); }, new Page.RouteOptions().setTimes(1)); Response response = page.navigate(server.EMPTY_PAGE); assertEquals("fulfilled one", response.text());