diff --git a/playwright/src/main/java/com/microsoft/playwright/Page.java b/playwright/src/main/java/com/microsoft/playwright/Page.java index 9defa636..e7156227 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Page.java +++ b/playwright/src/main/java/com/microsoft/playwright/Page.java @@ -2391,20 +2391,20 @@ public interface Page { *

Shortcut for main frame's [{@code method: Frame.waitForNavigation}]. */ Response waitForNavigation(Runnable code, WaitForNavigationOptions options); - Request waitForRequest(Runnable code); + default Request waitForRequest(Runnable code) { return waitForRequest(code, (Predicate) null, null); } default Request waitForRequest(Runnable code, String urlGlob) { return waitForRequest(code, urlGlob, null); } default Request waitForRequest(Runnable code, Pattern urlPattern) { return waitForRequest(code, urlPattern, null); } - default Request waitForRequest(Runnable code, Predicate urlPredicate) { return waitForRequest(code, urlPredicate, null); } + default Request waitForRequest(Runnable code, Predicate predicate) { return waitForRequest(code, predicate, null); } Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options); Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options); - Request waitForRequest(Runnable code, Predicate urlPredicate, WaitForRequestOptions options); - Response waitForResponse(Runnable code); + Request waitForRequest(Runnable code, Predicate predicate, WaitForRequestOptions options); + default Response waitForResponse(Runnable code) { return waitForResponse(code, (Predicate) null, null); } default Response waitForResponse(Runnable code, String urlGlob) { return waitForResponse(code, urlGlob, null); } default Response waitForResponse(Runnable code, Pattern urlPattern) { return waitForResponse(code, urlPattern, null); } - default Response waitForResponse(Runnable code, Predicate urlPredicate) { return waitForResponse(code, urlPredicate, null); } + default Response waitForResponse(Runnable code, Predicate predicate) { return waitForResponse(code, predicate, null); } Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options); Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options); - Response waitForResponse(Runnable code, Predicate urlPredicate, WaitForResponseOptions options); + Response waitForResponse(Runnable code, Predicate predicate, WaitForResponseOptions options); default ElementHandle waitForSelector(String selector) { return waitForSelector(selector, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java index 32a1d070..576def2c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -1180,11 +1180,6 @@ public class PageImpl extends ChannelOwner implements Page { return withLogging("Page.waitForNavigation", () -> waitForNavigationImpl(code, options)); } - @Override - public Request waitForRequest(Runnable code) { - return waitForRequest(code, UrlMatcher.any(), null); - } - Response waitForNavigationImpl(Runnable code, WaitForNavigationOptions options) { Frame.WaitForNavigationOptions frameOptions = new Frame.WaitForNavigationOptions(); if (options != null) { @@ -1288,34 +1283,30 @@ public class PageImpl extends ChannelOwner implements Page { @Override public Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options) { - return waitForRequest(code, new UrlMatcher(urlGlob), options); + return waitForRequest(code, toRequestPredicate(new UrlMatcher(urlGlob)), options); } @Override public Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options) { - return waitForRequest(code, new UrlMatcher(urlPattern), options); + return waitForRequest(code, toRequestPredicate(new UrlMatcher(urlPattern)), options); } @Override - public Request waitForRequest(Runnable code, Predicate urlPredicate, WaitForRequestOptions options) { - return waitForRequest(code, new UrlMatcher(urlPredicate), options); + public Request waitForRequest(Runnable code, Predicate predicate, WaitForRequestOptions options) { + return withLogging("Page.waitForRequest", () -> waitForRequestImpl(code, predicate, options)); } - @Override - public Response waitForResponse(Runnable code) { - return waitForResponse(code, UrlMatcher.any(), null); + private static Predicate toRequestPredicate(UrlMatcher matcher) { + return request -> matcher.test(request.url()); } - private Request waitForRequest(Runnable code, UrlMatcher matcher, WaitForRequestOptions options) { - return withLogging("Page.waitForRequest", () -> waitForRequestImpl(code, matcher, options)); - } - - private Request waitForRequestImpl(Runnable code, UrlMatcher matcher, WaitForRequestOptions options) { + private Request waitForRequestImpl(Runnable code, Predicate predicate, WaitForRequestOptions options) { if (options == null) { options = new WaitForRequestOptions(); } List> waitables = new ArrayList<>(); - waitables.add(new WaitableEvent<>(listeners, EventType.REQUEST, e -> matcher.test(((Request) e.data()).url())) + waitables.add(new WaitableEvent<>(listeners, EventType.REQUEST, + e -> predicate == null || predicate.test(((Request) e.data()))) .apply(event -> (Request) event.data())); waitables.add(createWaitForCloseHelper()); waitables.add(createWaitableTimeout(options.timeout)); @@ -1324,29 +1315,30 @@ public class PageImpl extends ChannelOwner implements Page { @Override public Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options) { - return waitForResponse(code, new UrlMatcher(urlGlob), options); + return waitForResponse(code, toResponsePredicate(new UrlMatcher(urlGlob)), options); } @Override public Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options) { - return waitForResponse(code, new UrlMatcher(urlPattern), options); + return waitForResponse(code, toResponsePredicate(new UrlMatcher(urlPattern)), options); } @Override - public Response waitForResponse(Runnable code, Predicate urlPredicate, WaitForResponseOptions options) { - return waitForResponse(code, new UrlMatcher(urlPredicate), options); + public Response waitForResponse(Runnable code, Predicate predicate, WaitForResponseOptions options) { + return withLogging("Page.waitForResponse", () -> waitForResponseImpl(code, predicate, options)); } - private Response waitForResponse(Runnable code, UrlMatcher matcher, WaitForResponseOptions options) { - return withLogging("Page.waitForResponse", () -> waitForonseImpl(code, matcher, options)); + private static Predicate toResponsePredicate(UrlMatcher matcher) { + return response -> matcher.test(response.url()); } - private Response waitForonseImpl(Runnable code, UrlMatcher matcher, WaitForResponseOptions options) { + private Response waitForResponseImpl(Runnable code, Predicate predicate, WaitForResponseOptions options) { if (options == null) { options = new WaitForResponseOptions(); } List> waitables = new ArrayList<>(); - waitables.add(new WaitableEvent<>(listeners, EventType.RESPONSE, e -> matcher.test(((Response) e.data()).url())) + waitables.add(new WaitableEvent<>(listeners, EventType.RESPONSE, + e -> predicate == null || predicate.test(((Response) e.data()))) .apply(event -> (Response) event.data())); waitables.add(createWaitForCloseHelper()); waitables.add(createWaitableTimeout(options.timeout)); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java index 0a125ca4..c0d18c02 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java @@ -46,7 +46,7 @@ public class TestPageWaitForRequest extends TestBase { " fetch('/digits/2.png');\n" + " fetch('/digits/3.png');\n" + "}"); - }, url -> url.equals(server.PREFIX + "/digits/2.png")); + }, r -> r.url().equals(server.PREFIX + "/digits/2.png")); assertEquals(server.PREFIX + "/digits/2.png", request.url()); } @@ -64,7 +64,7 @@ public class TestPageWaitForRequest extends TestBase { void shouldRespectDefaultTimeout() { page.setDefaultTimeout(1); try { - page.waitForRequest(() -> {}, url -> false); + page.waitForRequest(() -> {}, request -> false); fail("did not throw"); } catch (PlaywrightException e) { assertTrue(e.getMessage().contains("Timeout"), e.getMessage()); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java index 04001bb7..0125b911 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java @@ -43,7 +43,7 @@ public class TestPageWaitForResponse extends TestBase { " fetch('/digits/2.png');\n" + " fetch('/digits/3.png');\n" + "}"); - }, url -> url.equals(server.PREFIX + "/digits/2.png")); + }, r -> r.url().equals(server.PREFIX + "/digits/2.png")); assertEquals(server.PREFIX + "/digits/2.png", response.url()); } @@ -56,7 +56,7 @@ public class TestPageWaitForResponse extends TestBase { " fetch('/digits/2.png');\n" + " fetch('/digits/3.png');\n" + "}"); - }, url -> url.equals(server.PREFIX + "/digits/2.png")); + }, r -> r.url().equals(server.PREFIX + "/digits/2.png")); assertEquals(server.PREFIX + "/digits/2.png", response.url()); } @@ -64,7 +64,7 @@ public class TestPageWaitForResponse extends TestBase { void shouldRespectDefaultTimeout() { page.setDefaultTimeout(1); try { - page.waitForResponse(() -> {}, url -> false); + page.waitForResponse(() -> {}, response -> false); fail("did not throw"); } catch (PlaywrightException e) { assertTrue(e.getMessage().contains("Timeout"), e.getMessage()); diff --git a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 113595f0..9f132dae 100644 --- a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -583,22 +583,22 @@ class Method extends Element { customSignature.put("WebSocket.waitForEvent", new String[] {}); customSignature.put("Page.waitForRequest", new String[] { - "Request waitForRequest(Runnable code);", + "default Request waitForRequest(Runnable code) { return waitForRequest(code, (Predicate) null, null); }", "default Request waitForRequest(Runnable code, String urlGlob) { return waitForRequest(code, urlGlob, null); }", "default Request waitForRequest(Runnable code, Pattern urlPattern) { return waitForRequest(code, urlPattern, null); }", - "default Request waitForRequest(Runnable code, Predicate urlPredicate) { return waitForRequest(code, urlPredicate, null); }", + "default Request waitForRequest(Runnable code, Predicate predicate) { return waitForRequest(code, predicate, null); }", "Request waitForRequest(Runnable code, String urlGlob, WaitForRequestOptions options);", "Request waitForRequest(Runnable code, Pattern urlPattern, WaitForRequestOptions options);", - "Request waitForRequest(Runnable code, Predicate urlPredicate, WaitForRequestOptions options);" + "Request waitForRequest(Runnable code, Predicate predicate, WaitForRequestOptions options);" }); customSignature.put("Page.waitForResponse", new String[] { - "Response waitForResponse(Runnable code);", + "default Response waitForResponse(Runnable code) { return waitForResponse(code, (Predicate) null, null); }", "default Response waitForResponse(Runnable code, String urlGlob) { return waitForResponse(code, urlGlob, null); }", "default Response waitForResponse(Runnable code, Pattern urlPattern) { return waitForResponse(code, urlPattern, null); }", - "default Response waitForResponse(Runnable code, Predicate urlPredicate) { return waitForResponse(code, urlPredicate, null); }", + "default Response waitForResponse(Runnable code, Predicate predicate) { return waitForResponse(code, predicate, null); }", "Response waitForResponse(Runnable code, String urlGlob, WaitForResponseOptions options);", "Response waitForResponse(Runnable code, Pattern urlPattern, WaitForResponseOptions options);", - "Response waitForResponse(Runnable code, Predicate urlPredicate, WaitForResponseOptions options);" + "Response waitForResponse(Runnable code, Predicate predicate, WaitForResponseOptions options);" }); String[] waitForNavigation = { @@ -665,10 +665,6 @@ class Method extends Element { } private static Set skipJavadoc = new HashSet<>(asList( - "BrowserContext.waitForEvent.optionsOrPredicate", - "Page.waitForEvent.optionsOrPredicate", - "WebSocket.waitForEvent.optionsOrPredicate", - "Page.frame.options", "Page.waitForRequest", "Page.waitForResponse" ));