chore: only log har when pw:api is enabled (#977)

This commit is contained in:
Yury Semikhatsky 2022-06-28 16:23:04 -07:00 committed by GitHub
parent 9fac877892
commit 44cb76d92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 7 deletions

View File

@ -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.
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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) {
}

View File

@ -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());