mirror of
https://github.com/microsoft/playwright-java.git
synced 2026-03-31 20:02:48 +00:00
feat: protocol logging timestamps (#415)
This commit is contained in:
parent
c30e105e36
commit
627a940bcd
@ -36,6 +36,7 @@ import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.microsoft.playwright.impl.LoggingSupport.logWithTimestamp;
|
||||
import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
|
||||
class Message {
|
||||
@ -67,7 +68,11 @@ public class Connection {
|
||||
private int lastId = 0;
|
||||
private final Path srcDir;
|
||||
private final Map<Integer, WaitableResult<JsonElement>> callbacks = new HashMap<>();
|
||||
private final boolean isLogging = "pw:channel".equals(System.getenv("DEBUG"));
|
||||
private static final boolean isLogging;
|
||||
static {
|
||||
String debug = System.getenv("DEBUG");
|
||||
isLogging = (debug != null) && debug.contains("pw:channel");
|
||||
}
|
||||
|
||||
class Root extends ChannelOwner {
|
||||
Root(Connection connection) {
|
||||
@ -156,7 +161,7 @@ public class Connection {
|
||||
}
|
||||
String messageString = gson().toJson(message);
|
||||
if (isLogging) {
|
||||
System.err.println("SEND ► " + messageString);
|
||||
logWithTimestamp("SEND ► " + messageString);
|
||||
}
|
||||
transport.send(messageString);
|
||||
return result;
|
||||
@ -190,7 +195,7 @@ public class Connection {
|
||||
return;
|
||||
}
|
||||
if (isLogging) {
|
||||
System.err.println("◀ RECV " + messageString);
|
||||
logWithTimestamp("◀ RECV " + messageString);
|
||||
}
|
||||
Gson gson = gson();
|
||||
Message message = gson.fromJson(messageString, Message.class);
|
||||
|
||||
@ -54,9 +54,14 @@ class LoggingSupport {
|
||||
}
|
||||
}
|
||||
|
||||
private void logApi(String message) {
|
||||
static void logWithTimestamp(String message) {
|
||||
// This matches log format produced by the server.
|
||||
String timestamp = ZonedDateTime.now().format(timestampFormat);
|
||||
System.err.println(timestamp + " pw:api " + message);
|
||||
System.err.println(timestamp + " " + message);
|
||||
}
|
||||
|
||||
private void logApi(String message) {
|
||||
// This matches log format produced by the server.
|
||||
System.err.println("pw:api " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +41,8 @@ public class TestChromium extends TestBase {
|
||||
// Do not create anything.
|
||||
}
|
||||
|
||||
private static int nextPort = 9339;
|
||||
|
||||
private static String wsEndpointFromUrl(String urlString) throws IOException {
|
||||
URL url = new URL(urlString);
|
||||
URLConnection request = url.openConnection();
|
||||
@ -52,7 +54,7 @@ public class TestChromium extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldConnectToAnExistingCdpSession() throws IOException {
|
||||
int port = 9339;
|
||||
int port = nextPort++;
|
||||
try (Browser browserServer = browserType.launch(createLaunchOptions()
|
||||
.setArgs(asList("--remote-debugging-port=" + port)))) {
|
||||
Browser cdpBrowser = browserType.connectOverCDP("http://localhost:" + port);
|
||||
@ -64,7 +66,7 @@ public class TestChromium extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldConnectToAnExistingCdpSessionTwice() throws IOException {
|
||||
int port = 9339;
|
||||
int port = nextPort++;
|
||||
try (Browser browserServer = browserType.launch(createLaunchOptions()
|
||||
.setArgs(asList("--remote-debugging-port=" + port)))) {
|
||||
String endpointUrl = "http://localhost:" + port;
|
||||
@ -90,7 +92,7 @@ public class TestChromium extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldConnectOverAWsEndpoint() throws IOException {
|
||||
int port = 9339;
|
||||
int port = nextPort++;
|
||||
try (Browser browserServer = browserType.launch(createLaunchOptions()
|
||||
.setArgs(asList("--remote-debugging-port=" + port)))) {
|
||||
String wsEndpoint = wsEndpointFromUrl("http://localhost:" + port + "/json/version/");
|
||||
|
||||
@ -22,15 +22,19 @@ import org.java_websocket.server.WebSocketServer;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class WebSocketServerImpl extends WebSocketServer implements AutoCloseable {
|
||||
volatile ClientHandshake lastClientHandshake;
|
||||
private final Semaphore startSemaphore = new Semaphore(0);
|
||||
|
||||
static final int WS_SERVER_PORT = 8910;
|
||||
private static final AtomicInteger nextWsServerPort = new AtomicInteger(8910);
|
||||
|
||||
static WebSocketServerImpl create() throws InterruptedException {
|
||||
WebSocketServerImpl result = new WebSocketServerImpl(new InetSocketAddress("localhost", WebSocketServerImpl.WS_SERVER_PORT));
|
||||
// FIXME: WebSocketServer.stop() doesn't release socket immediately and starting another server
|
||||
// fails with "Address already in use", so we just allocate new port.
|
||||
int port = nextWsServerPort.getAndIncrement();
|
||||
WebSocketServerImpl result = new WebSocketServerImpl(new InetSocketAddress("localhost", port));
|
||||
result.start();
|
||||
result.startSemaphore.acquire();
|
||||
return result;
|
||||
@ -61,6 +65,8 @@ class WebSocketServerImpl extends WebSocketServer implements AutoCloseable {
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket webSocket, Exception e) {
|
||||
e.printStackTrace();
|
||||
startSemaphore.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user