Skip to content

Commit 5883d77

Browse files
authored
fix(tracing): title the waits in the trace like the Node.js client (#2023)
1 parent 2ebc19a commit 5883d77

9 files changed

Lines changed: 77 additions & 22 deletions

File tree

‎playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private <T> T waitForEventWithTimeout(EventType eventType, Runnable code, Predic
342342

343343
@Override
344344
public Page waitForPage(WaitForPageOptions options, Runnable code) {
345-
return withWaitLogging("BrowserContext.close", logger -> waitForPageImpl(options, code));
345+
return withWaitLogging("BrowserContext.waitForPage", "page", logger -> waitForPageImpl(options, code));
346346
}
347347

348348
private Page waitForPageImpl(WaitForPageOptions options, Runnable code) {
@@ -706,7 +706,7 @@ public void waitForCondition(BooleanSupplier predicate, WaitForConditionOptions
706706

707707
@Override
708708
public ConsoleMessage waitForConsoleMessage(WaitForConsoleMessageOptions options, Runnable code) {
709-
return withWaitLogging("BrowserContext.waitForConsoleMessage", logger -> waitForConsoleMessageImpl(options, code));
709+
return withWaitLogging("BrowserContext.waitForConsoleMessage", "console", logger -> waitForConsoleMessageImpl(options, code));
710710
}
711711

712712
private ConsoleMessage waitForConsoleMessageImpl(WaitForConsoleMessageOptions options, Runnable code) {

‎playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ void adopt(ChannelOwner child) {
8181
}
8282

8383
<T> T withWaitLogging(String apiName, Function<Logger, T> code) {
84-
return new WaitForEventLogger<>(this, apiName, code).get();
84+
return new WaitForEventLogger<>(this, apiName, null, null, code).get();
85+
}
86+
87+
/** A wait for the event, titled {@code Wait for event "<event>"} in the trace like the Node.js client's waitForEvent. */
88+
<T> T withWaitLogging(String apiName, String event, Function<Logger, T> code) {
89+
return new WaitForEventLogger<>(this, apiName, event, "Wait for event \"" + event + "\"", code).get();
90+
}
91+
92+
<T> T withWaitLoggingTitled(String apiName, String title, Function<Logger, T> code) {
93+
return new WaitForEventLogger<>(this, apiName, null, title, code).get();
8594
}
8695

8796

‎playwright/src/main/java/com/microsoft/playwright/impl/Connection.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ void setIsTracing(boolean tracing) {
131131
}
132132
}
133133

134+
String title() {
135+
return title;
136+
}
137+
134138
String setTitle(String newTitle) {
135139
String previous = title;
136140
titleReported = false;

‎playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ public Response get() {
961961

962962
@Override
963963
public Response waitForNavigation(WaitForNavigationOptions options, Runnable code) {
964-
return withWaitLogging("Frame.waitForNavigation", logger -> waitForNavigationImpl(logger, code, options, null));
964+
return withWaitLoggingTitled("Frame.waitForNavigation", "Wait for navigation", logger -> waitForNavigationImpl(logger, code, options, null));
965965
}
966966

967967
Response waitForNavigationImpl(Logger logger, Runnable code, WaitForNavigationOptions options) {

‎playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public ClockImpl clock() {
469469

470470
@Override
471471
public Page waitForClose(WaitForCloseOptions options, Runnable code) {
472-
return withWaitLogging("Page.waitForClose", logger -> waitForCloseImpl(options, code));
472+
return withWaitLogging("Page.waitForClose", "close", logger -> waitForCloseImpl(options, code));
473473
}
474474

475475
private Page waitForCloseImpl(WaitForCloseOptions options, Runnable code) {
@@ -481,7 +481,7 @@ private Page waitForCloseImpl(WaitForCloseOptions options, Runnable code) {
481481

482482
@Override
483483
public ConsoleMessage waitForConsoleMessage(WaitForConsoleMessageOptions options, Runnable code) {
484-
return withWaitLogging("Page.waitForConsoleMessage", logger -> waitForConsoleMessageImpl(options, code));
484+
return withWaitLogging("Page.waitForConsoleMessage", "console", logger -> waitForConsoleMessageImpl(options, code));
485485
}
486486

487487
private ConsoleMessage waitForConsoleMessageImpl(WaitForConsoleMessageOptions options, Runnable code) {
@@ -493,7 +493,7 @@ private ConsoleMessage waitForConsoleMessageImpl(WaitForConsoleMessageOptions op
493493

494494
@Override
495495
public Download waitForDownload(WaitForDownloadOptions options, Runnable code) {
496-
return withWaitLogging("Page.waitForDownload", logger -> waitForDownloadImpl(options, code));
496+
return withWaitLogging("Page.waitForDownload", "download", logger -> waitForDownloadImpl(options, code));
497497
}
498498

499499
private Download waitForDownloadImpl(WaitForDownloadOptions options, Runnable code) {
@@ -505,7 +505,7 @@ private Download waitForDownloadImpl(WaitForDownloadOptions options, Runnable co
505505

506506
@Override
507507
public FileChooser waitForFileChooser(WaitForFileChooserOptions options, Runnable code) {
508-
return withWaitLogging("Page.waitForFileChooser", logger -> waitForFileChooserImpl(options, code));
508+
return withWaitLogging("Page.waitForFileChooser", "filechooser", logger -> waitForFileChooserImpl(options, code));
509509
}
510510

511511
private FileChooser waitForFileChooserImpl(WaitForFileChooserOptions options, Runnable code) {
@@ -518,7 +518,7 @@ private FileChooser waitForFileChooserImpl(WaitForFileChooserOptions options, Ru
518518

519519
@Override
520520
public Page waitForPopup(WaitForPopupOptions options, Runnable code) {
521-
return withWaitLogging("Page.waitForPopup", logger -> waitForPopupImpl(options, code));
521+
return withWaitLogging("Page.waitForPopup", "popup", logger -> waitForPopupImpl(options, code));
522522
}
523523

524524
private Page waitForPopupImpl(WaitForPopupOptions options, Runnable code) {
@@ -530,7 +530,7 @@ private Page waitForPopupImpl(WaitForPopupOptions options, Runnable code) {
530530

531531
@Override
532532
public WebSocket waitForWebSocket(WaitForWebSocketOptions options, Runnable code) {
533-
return withWaitLogging("Page.waitForWebSocket", logger -> waitForWebSocketImpl(options, code));
533+
return withWaitLogging("Page.waitForWebSocket", "websocket", logger -> waitForWebSocketImpl(options, code));
534534
}
535535

536536
private WebSocket waitForWebSocketImpl(WaitForWebSocketOptions options, Runnable code) {
@@ -542,7 +542,7 @@ private WebSocket waitForWebSocketImpl(WaitForWebSocketOptions options, Runnable
542542

543543
@Override
544544
public Worker waitForWorker(WaitForWorkerOptions options, Runnable code) {
545-
return withWaitLogging("Page.waitForWorker", logger -> waitForWorkerImpl(options, code));
545+
return withWaitLogging("Page.waitForWorker", "worker", logger -> waitForWorkerImpl(options, code));
546546
}
547547

548548
private Worker waitForWorkerImpl(WaitForWorkerOptions options, Runnable code) {
@@ -1477,7 +1477,7 @@ public void waitForLoadState(LoadState state, WaitForLoadStateOptions options) {
14771477

14781478
@Override
14791479
public Response waitForNavigation(WaitForNavigationOptions options, Runnable code) {
1480-
return withWaitLogging("Page.waitForNavigation", logger -> waitForNavigationImpl(logger, code, options));
1480+
return withWaitLoggingTitled("Page.waitForNavigation", "Wait for navigation", logger -> waitForNavigationImpl(logger, code, options));
14811481
}
14821482

14831483
private Response waitForNavigationImpl(Logger logger, Runnable code, WaitForNavigationOptions options) {
@@ -1554,7 +1554,7 @@ public Request waitForRequest(Predicate<Request> predicate, WaitForRequestOption
15541554
}
15551555

15561556
private Request waitForRequest(UrlMatcher urlMatcher, Predicate<Request> predicate, WaitForRequestOptions options, Runnable code) {
1557-
return withWaitLogging("Page.waitForRequest", logger -> {
1557+
return withWaitLogging("Page.waitForRequest", "request", logger -> {
15581558
logger.log("waiting for request " + ((urlMatcher == null) ? "matching predicate" : urlMatcher.toString()));
15591559
Predicate<Request> requestPredicate = predicate;
15601560
if (requestPredicate == null) {
@@ -1573,7 +1573,7 @@ private Request waitForRequestImpl(Predicate<Request> predicate, WaitForRequestO
15731573

15741574
@Override
15751575
public Request waitForRequestFinished(WaitForRequestFinishedOptions options, Runnable code) {
1576-
return withWaitLogging("Page.waitForRequestFinished", logger -> waitForRequestFinishedImpl(options, code));
1576+
return withWaitLogging("Page.waitForRequestFinished", "requestfinished", logger -> waitForRequestFinishedImpl(options, code));
15771577
}
15781578

15791579
private Request waitForRequestFinishedImpl(WaitForRequestFinishedOptions options, Runnable code) {
@@ -1599,7 +1599,7 @@ public Response waitForResponse(Predicate<Response> predicate, WaitForResponseOp
15991599
}
16001600

16011601
private Response waitForResponse(UrlMatcher urlMatcher, Predicate<Response> predicate, WaitForResponseOptions options, Runnable code) {
1602-
return withWaitLogging("Page.waitForResponse", logger -> {
1602+
return withWaitLogging("Page.waitForResponse", "response", logger -> {
16031603
logger.log("waiting for response " + ((urlMatcher == null) ? "matching predicate" : urlMatcher.toString()));
16041604
Predicate<Response> responsePredicate = predicate;
16051605
if (responsePredicate == null) {

‎playwright/src/main/java/com/microsoft/playwright/impl/WaitForEventLogger.java‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ public class WaitForEventLogger<T> implements Supplier<T>, Logger {
2828
private final ChannelOwner channel;
2929
private final String waitId;
3030
private final String apiName;
31+
private final String event;
32+
private final String title;
3133

32-
WaitForEventLogger(ChannelOwner channelOwner, String apiName, Function<Logger, T> supplier) {
34+
WaitForEventLogger(ChannelOwner channelOwner, String apiName, String event, String title, Function<Logger, T> supplier) {
3335
this.supplier = supplier;
3436
this.channel = channelOwner;
3537
this.apiName = apiName;
38+
this.event = event;
39+
this.title = title;
3640
this.waitId = createGuid();
3741
}
3842

@@ -41,8 +45,8 @@ public T get() {
4145
{
4246
JsonObject info = new JsonObject();
4347
info.addProperty("phase", "before");
44-
info.addProperty("event", "");
45-
sendWaitInfo(info);
48+
info.addProperty("event", event == null ? "" : event);
49+
sendWaitInfo(info, title);
4650
}
4751
JsonObject info = new JsonObject();
4852
info.addProperty("phase", "after");
@@ -66,11 +70,27 @@ public void log(String message) {
6670
}
6771

6872
private void sendWaitInfo(JsonObject info) {
73+
sendWaitInfo(info, null);
74+
}
75+
76+
// The title goes with the first message of the wait, as in the Node.js client, where it is the
77+
// title of the api zone; an outer call that has a title of its own keeps it. It is set for that
78+
// message only, so that the calls made from the callback of the wait are not hidden as internal.
79+
private void sendWaitInfo(JsonObject info, String title) {
6980
info.addProperty("waitId", waitId);
81+
Connection connection = channel.connection;
82+
boolean setTitle = title != null && connection.title() == null;
83+
if (setTitle) {
84+
connection.setTitle(title);
85+
}
7086
try {
7187
channel.sendMessageNoReply("__waitInfo__", info);
7288
} catch (RuntimeException e) {
7389
// Fire-and-forget: never throw to the caller.
90+
} finally {
91+
if (setTitle) {
92+
connection.setTitle(null);
93+
}
7494
}
7595
}
7696
}

‎playwright/src/main/java/com/microsoft/playwright/impl/WebSocketImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void offSocketError(Consumer<String> handler) {
8585

8686
@Override
8787
public WebSocketFrame waitForFrameReceived(WaitForFrameReceivedOptions options, Runnable code) {
88-
return withWaitLogging("WebSocket.waitForFrameReceived", logger -> waitForFrameReceivedImpl(options, code));
88+
return withWaitLogging("WebSocket.waitForFrameReceived", "framereceived", logger -> waitForFrameReceivedImpl(options, code));
8989
}
9090

9191
private WebSocketFrame waitForFrameReceivedImpl(WaitForFrameReceivedOptions options, Runnable code) {
@@ -97,7 +97,7 @@ private WebSocketFrame waitForFrameReceivedImpl(WaitForFrameReceivedOptions opti
9797

9898
@Override
9999
public WebSocketFrame waitForFrameSent(WaitForFrameSentOptions options, Runnable code) {
100-
return withWaitLogging("WebSocket.waitForFrameSent", logger -> waitForFrameSentImpl(options, code));
100+
return withWaitLogging("WebSocket.waitForFrameSent", "framesent", logger -> waitForFrameSentImpl(options, code));
101101
}
102102

103103
private WebSocketFrame waitForFrameSentImpl(WaitForFrameSentOptions options, Runnable code) {

‎playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ private <T> T waitForEventWithTimeout(EventType eventType, Runnable code, Predic
7373

7474
@Override
7575
public Worker waitForClose(WaitForCloseOptions options, Runnable code) {
76-
return withWaitLogging("Worker.waitForClose", logger -> waitForCloseImpl(options, code));
76+
return withWaitLogging("Worker.waitForClose", "close", logger -> waitForCloseImpl(options, code));
7777
}
7878

7979
@Override
8080
public ConsoleMessage waitForConsoleMessage(WaitForConsoleMessageOptions options, Runnable code) {
81-
return withWaitLogging("Worker.waitForConsoleMessage", logger -> waitForConsoleMessageImpl(options, code));
81+
return withWaitLogging("Worker.waitForConsoleMessage", "console", logger -> waitForConsoleMessageImpl(options, code));
8282
}
8383

8484
private ConsoleMessage waitForConsoleMessageImpl(WaitForConsoleMessageOptions options, Runnable code) {

‎playwright/src/test/java/com/microsoft/playwright/TestTracing.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,28 @@ public void shouldShowWaitForLoadState(Page page, BrowserContext context, Browse
375375
});
376376
}
377377

378+
@PlaywrightTest
379+
public void shouldShowWaitForEventAndNavigation(Page page, BrowserContext context, BrowserType browserType, Server server, @TempDir Path tempDir) throws Exception {
380+
context.tracing().start(new Tracing.StartOptions());
381+
382+
page.navigate(server.EMPTY_PAGE);
383+
page.waitForConsoleMessage(() -> page.evaluate("() => console.log('hello')"));
384+
page.waitForNavigation(() -> page.evaluate("() => window.location.href = '/one-style.html'"));
385+
386+
Path traceFile = tempDir.resolve("trace.zip");
387+
context.tracing().stop(new Tracing.StopOptions().setPath(traceFile));
388+
389+
TraceViewerPage.showTraceViewer(browserType, traceFile, traceViewer -> {
390+
assertThat(traceViewer.actionTitles()).hasText(new Pattern[] {
391+
Pattern.compile("Navigate.*/empty.html"),
392+
Pattern.compile("Wait for event \"console\""),
393+
Pattern.compile("Evaluate"),
394+
Pattern.compile("Wait for navigation"),
395+
Pattern.compile("Evaluate"),
396+
});
397+
});
398+
}
399+
378400
@PlaywrightTest
379401
public void shouldRecordHarWithStartHarStopHar(Page page, BrowserContext context, Server server, @TempDir Path tempDir) throws Exception {
380402
Path harPath = tempDir.resolve("tracing.har");

0 commit comments

Comments
 (0)