Skip to content

Commit d1f1287

Browse files
authored
fix(api): update waitForRequest/Response predicate to accept Request/… (#224)
1 parent 0c30cc1 commit d1f1287

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

playwright/src/main/java/com/microsoft/playwright/Page.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,20 +2391,20 @@ default void waitForLoadState() {
23912391
* <p> Shortcut for main frame's [{@code method: Frame.waitForNavigation}].
23922392
*/
23932393
Response waitForNavigation(Runnable code, WaitForNavigationOptions options);
2394-
Request waitForRequest(Runnable code);
2394+
default Request waitForRequest(Runnable code) { return waitForRequest(code, (Predicate<Request>) null, null); }
23952395
default Request waitForRequest(Runnable code, String urlGlob) { return waitForRequest(code, urlGlob, null); }
23962396
default Request waitForRequest(Runnable code, Pattern urlPattern) { return waitForRequest(code, urlPattern, null); }
2397-
default Request waitForRequest(Runnable code, Predicate<String> urlPredicate) { return waitForRequest(code, urlPredicate, null); }
2397+
default Request waitForRequest(Runnable code, Predicate<Request> predicate) { return waitForRequest(code, predicate, null); }
23982398
Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options);
23992399
Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options);
2400-
Request waitForRequest(Runnable code, Predicate<String> urlPredicate, WaitForRequestOptions options);
2401-
Response waitForResponse(Runnable code);
2400+
Request waitForRequest(Runnable code, Predicate<Request> predicate, WaitForRequestOptions options);
2401+
default Response waitForResponse(Runnable code) { return waitForResponse(code, (Predicate<Response>) null, null); }
24022402
default Response waitForResponse(Runnable code, String urlGlob) { return waitForResponse(code, urlGlob, null); }
24032403
default Response waitForResponse(Runnable code, Pattern urlPattern) { return waitForResponse(code, urlPattern, null); }
2404-
default Response waitForResponse(Runnable code, Predicate<String> urlPredicate) { return waitForResponse(code, urlPredicate, null); }
2404+
default Response waitForResponse(Runnable code, Predicate<Response> predicate) { return waitForResponse(code, predicate, null); }
24052405
Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options);
24062406
Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options);
2407-
Response waitForResponse(Runnable code, Predicate<String> urlPredicate, WaitForResponseOptions options);
2407+
Response waitForResponse(Runnable code, Predicate<Response> predicate, WaitForResponseOptions options);
24082408
default ElementHandle waitForSelector(String selector) {
24092409
return waitForSelector(selector, null);
24102410
}

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,6 @@ public Response waitForNavigation(Runnable code, WaitForNavigationOptions option
11801180
return withLogging("Page.waitForNavigation", () -> waitForNavigationImpl(code, options));
11811181
}
11821182

1183-
@Override
1184-
public Request waitForRequest(Runnable code) {
1185-
return waitForRequest(code, UrlMatcher.any(), null);
1186-
}
1187-
11881183
Response waitForNavigationImpl(Runnable code, WaitForNavigationOptions options) {
11891184
Frame.WaitForNavigationOptions frameOptions = new Frame.WaitForNavigationOptions();
11901185
if (options != null) {
@@ -1288,34 +1283,30 @@ public void dispose() {
12881283

12891284
@Override
12901285
public Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options) {
1291-
return waitForRequest(code, new UrlMatcher(urlGlob), options);
1286+
return waitForRequest(code, toRequestPredicate(new UrlMatcher(urlGlob)), options);
12921287
}
12931288

12941289
@Override
12951290
public Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options) {
1296-
return waitForRequest(code, new UrlMatcher(urlPattern), options);
1297-
}
1298-
1299-
@Override
1300-
public Request waitForRequest(Runnable code, Predicate<String> urlPredicate, WaitForRequestOptions options) {
1301-
return waitForRequest(code, new UrlMatcher(urlPredicate), options);
1291+
return waitForRequest(code, toRequestPredicate(new UrlMatcher(urlPattern)), options);
13021292
}
13031293

13041294
@Override
1305-
public Response waitForResponse(Runnable code) {
1306-
return waitForResponse(code, UrlMatcher.any(), null);
1295+
public Request waitForRequest(Runnable code, Predicate<Request> predicate, WaitForRequestOptions options) {
1296+
return withLogging("Page.waitForRequest", () -> waitForRequestImpl(code, predicate, options));
13071297
}
13081298

1309-
private Request waitForRequest(Runnable code, UrlMatcher matcher, WaitForRequestOptions options) {
1310-
return withLogging("Page.waitForRequest", () -> waitForRequestImpl(code, matcher, options));
1299+
private static Predicate<Request> toRequestPredicate(UrlMatcher matcher) {
1300+
return request -> matcher.test(request.url());
13111301
}
13121302

1313-
private Request waitForRequestImpl(Runnable code, UrlMatcher matcher, WaitForRequestOptions options) {
1303+
private Request waitForRequestImpl(Runnable code, Predicate<Request> predicate, WaitForRequestOptions options) {
13141304
if (options == null) {
13151305
options = new WaitForRequestOptions();
13161306
}
13171307
List<Waitable<Request>> waitables = new ArrayList<>();
1318-
waitables.add(new WaitableEvent<>(listeners, EventType.REQUEST, e -> matcher.test(((Request) e.data()).url()))
1308+
waitables.add(new WaitableEvent<>(listeners, EventType.REQUEST,
1309+
e -> predicate == null || predicate.test(((Request) e.data())))
13191310
.apply(event -> (Request) event.data()));
13201311
waitables.add(createWaitForCloseHelper());
13211312
waitables.add(createWaitableTimeout(options.timeout));
@@ -1324,29 +1315,30 @@ private Request waitForRequestImpl(Runnable code, UrlMatcher matcher, WaitForReq
13241315

13251316
@Override
13261317
public Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options) {
1327-
return waitForResponse(code, new UrlMatcher(urlGlob), options);
1318+
return waitForResponse(code, toResponsePredicate(new UrlMatcher(urlGlob)), options);
13281319
}
13291320

13301321
@Override
13311322
public Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options) {
1332-
return waitForResponse(code, new UrlMatcher(urlPattern), options);
1323+
return waitForResponse(code, toResponsePredicate(new UrlMatcher(urlPattern)), options);
13331324
}
13341325

13351326
@Override
1336-
public Response waitForResponse(Runnable code, Predicate<String> urlPredicate, WaitForResponseOptions options) {
1337-
return waitForResponse(code, new UrlMatcher(urlPredicate), options);
1327+
public Response waitForResponse(Runnable code, Predicate<Response> predicate, WaitForResponseOptions options) {
1328+
return withLogging("Page.waitForResponse", () -> waitForResponseImpl(code, predicate, options));
13381329
}
13391330

1340-
private Response waitForResponse(Runnable code, UrlMatcher matcher, WaitForResponseOptions options) {
1341-
return withLogging("Page.waitForResponse", () -> waitForonseImpl(code, matcher, options));
1331+
private static Predicate<Response> toResponsePredicate(UrlMatcher matcher) {
1332+
return response -> matcher.test(response.url());
13421333
}
13431334

1344-
private Response waitForonseImpl(Runnable code, UrlMatcher matcher, WaitForResponseOptions options) {
1335+
private Response waitForResponseImpl(Runnable code, Predicate<Response> predicate, WaitForResponseOptions options) {
13451336
if (options == null) {
13461337
options = new WaitForResponseOptions();
13471338
}
13481339
List<Waitable<Response>> waitables = new ArrayList<>();
1349-
waitables.add(new WaitableEvent<>(listeners, EventType.RESPONSE, e -> matcher.test(((Response) e.data()).url()))
1340+
waitables.add(new WaitableEvent<>(listeners, EventType.RESPONSE,
1341+
e -> predicate == null || predicate.test(((Response) e.data())))
13501342
.apply(event -> (Response) event.data()));
13511343
waitables.add(createWaitForCloseHelper());
13521344
waitables.add(createWaitableTimeout(options.timeout));

playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void shouldWorkWithPredicate() {
4646
" fetch('/digits/2.png');\n" +
4747
" fetch('/digits/3.png');\n" +
4848
"}");
49-
}, url -> url.equals(server.PREFIX + "/digits/2.png"));
49+
}, r -> r.url().equals(server.PREFIX + "/digits/2.png"));
5050
assertEquals(server.PREFIX + "/digits/2.png", request.url());
5151
}
5252

@@ -64,7 +64,7 @@ void shouldRespectTimeout() {
6464
void shouldRespectDefaultTimeout() {
6565
page.setDefaultTimeout(1);
6666
try {
67-
page.waitForRequest(() -> {}, url -> false);
67+
page.waitForRequest(() -> {}, request -> false);
6868
fail("did not throw");
6969
} catch (PlaywrightException e) {
7070
assertTrue(e.getMessage().contains("Timeout"), e.getMessage());

playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void shouldRespectTimeout() {
4343
" fetch('/digits/2.png');\n" +
4444
" fetch('/digits/3.png');\n" +
4545
"}");
46-
}, url -> url.equals(server.PREFIX + "/digits/2.png"));
46+
}, r -> r.url().equals(server.PREFIX + "/digits/2.png"));
4747
assertEquals(server.PREFIX + "/digits/2.png", response.url());
4848
}
4949

@@ -56,15 +56,15 @@ void shouldWorkWithPredicate() {
5656
" fetch('/digits/2.png');\n" +
5757
" fetch('/digits/3.png');\n" +
5858
"}");
59-
}, url -> url.equals(server.PREFIX + "/digits/2.png"));
59+
}, r -> r.url().equals(server.PREFIX + "/digits/2.png"));
6060
assertEquals(server.PREFIX + "/digits/2.png", response.url());
6161
}
6262

6363
@Test
6464
void shouldRespectDefaultTimeout() {
6565
page.setDefaultTimeout(1);
6666
try {
67-
page.waitForResponse(() -> {}, url -> false);
67+
page.waitForResponse(() -> {}, response -> false);
6868
fail("did not throw");
6969
} catch (PlaywrightException e) {
7070
assertTrue(e.getMessage().contains("Timeout"), e.getMessage());

tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -583,22 +583,22 @@ class Method extends Element {
583583
customSignature.put("WebSocket.waitForEvent", new String[] {});
584584

585585
customSignature.put("Page.waitForRequest", new String[] {
586-
"Request waitForRequest(Runnable code);",
586+
"default Request waitForRequest(Runnable code) { return waitForRequest(code, (Predicate<Request>) null, null); }",
587587
"default Request waitForRequest(Runnable code, String urlGlob) { return waitForRequest(code, urlGlob, null); }",
588588
"default Request waitForRequest(Runnable code, Pattern urlPattern) { return waitForRequest(code, urlPattern, null); }",
589-
"default Request waitForRequest(Runnable code, Predicate<String> urlPredicate) { return waitForRequest(code, urlPredicate, null); }",
589+
"default Request waitForRequest(Runnable code, Predicate<Request> predicate) { return waitForRequest(code, predicate, null); }",
590590
"Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options);",
591591
"Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options);",
592-
"Request waitForRequest(Runnable code, Predicate<String> urlPredicate, WaitForRequestOptions options);"
592+
"Request waitForRequest(Runnable code, Predicate<Request> predicate, WaitForRequestOptions options);"
593593
});
594594
customSignature.put("Page.waitForResponse", new String[] {
595-
"Response waitForResponse(Runnable code);",
595+
"default Response waitForResponse(Runnable code) { return waitForResponse(code, (Predicate<Response>) null, null); }",
596596
"default Response waitForResponse(Runnable code, String urlGlob) { return waitForResponse(code, urlGlob, null); }",
597597
"default Response waitForResponse(Runnable code, Pattern urlPattern) { return waitForResponse(code, urlPattern, null); }",
598-
"default Response waitForResponse(Runnable code, Predicate<String> urlPredicate) { return waitForResponse(code, urlPredicate, null); }",
598+
"default Response waitForResponse(Runnable code, Predicate<Response> predicate) { return waitForResponse(code, predicate, null); }",
599599
"Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options);",
600600
"Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options);",
601-
"Response waitForResponse(Runnable code, Predicate<String> urlPredicate, WaitForResponseOptions options);"
601+
"Response waitForResponse(Runnable code, Predicate<Response> predicate, WaitForResponseOptions options);"
602602
});
603603

604604
String[] waitForNavigation = {
@@ -665,10 +665,6 @@ class Method extends Element {
665665
}
666666

667667
private static Set<String> skipJavadoc = new HashSet<>(asList(
668-
"BrowserContext.waitForEvent.optionsOrPredicate",
669-
"Page.waitForEvent.optionsOrPredicate",
670-
"WebSocket.waitForEvent.optionsOrPredicate",
671-
"Page.frame.options",
672668
"Page.waitForRequest",
673669
"Page.waitForResponse"
674670
));

0 commit comments

Comments
 (0)