From 3376836752a0f480c4875044d4b7ccf5ee85d32b Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 20 Jan 2021 19:32:53 -0800 Subject: [PATCH] fix(api): remove Listener interface from public API (#219) --- .../com/microsoft/playwright/Browser.java | 6 --- .../microsoft/playwright/BrowserContext.java | 7 ---- .../java/com/microsoft/playwright/Page.java | 24 ------------ .../com/microsoft/playwright/WebSocket.java | 9 ----- .../java/com/microsoft/playwright/Worker.java | 6 --- .../playwright/impl/BrowserContextImpl.java | 20 ++++------ .../playwright/impl/BrowserImpl.java | 14 ++----- .../playwright/{ => impl}/Listener.java | 4 +- .../playwright/impl/ListenerCollection.java | 3 +- .../microsoft/playwright/impl/PageImpl.java | 39 ++++++++++++------- .../playwright/impl/WaitableEvent.java | 2 - .../playwright/impl/WebSocketImpl.java | 23 +++++------ .../microsoft/playwright/impl/WorkerImpl.java | 14 ++----- .../com/microsoft/playwright/TestBase.java | 5 ++- .../playwright/TestBrowserContextBasic.java | 4 +- .../TestBrowserContextCredentials.java | 1 - .../playwright/TestBrowserContextRoute.java | 1 - .../com/microsoft/playwright/TestClick.java | 4 +- .../TestDefaultBrowserContext2.java | 2 +- .../com/microsoft/playwright/TestDialog.java | 17 +++----- .../playwright/TestDispatchEvent.java | 3 +- .../microsoft/playwright/TestDownload.java | 25 ++++++------ .../TestElementHandleSelectText.java | 3 -- .../microsoft/playwright/TestGeolocation.java | 4 +- .../com/microsoft/playwright/TestHar.java | 2 - .../playwright/TestNetworkRequest.java | 23 +++++------ .../playwright/TestNetworkResponse.java | 8 +--- .../microsoft/playwright/TestPageBasic.java | 5 +-- .../playwright/TestPageEmulateMedia.java | 1 - .../playwright/TestPageEvaluate.java | 8 +--- .../playwright/TestPageEventNetwork.java | 23 +++++------ .../microsoft/playwright/TestPageRoute.java | 31 +++++++-------- .../playwright/TestPageSelectOption.java | 1 - .../playwright/TestPageSetInputFiles.java | 27 ++++++------- .../playwright/TestPageWaitForNavigation.java | 11 ++---- .../playwright/TestPageWaitForRequest.java | 1 - .../playwright/TestPageWaitForResponse.java | 2 - .../com/microsoft/playwright/TestPdf.java | 1 - .../com/microsoft/playwright/TestPopup.java | 1 - .../playwright/TestRequestContinue.java | 1 - .../playwright/TestRequestFulfill.java | 1 - .../playwright/TestSelectorsRegister.java | 1 - .../com/microsoft/playwright/TestTap.java | 1 - .../playwright/TestWaitForFunction.java | 12 +++--- .../microsoft/playwright/TestWebSocket.java | 37 +++++++----------- .../com/microsoft/playwright/TestWorkers.java | 3 +- .../java/com/microsoft/playwright/Utils.java | 1 - .../playwright/tools/ApiGenerator.java | 10 ----- 48 files changed, 155 insertions(+), 297 deletions(-) rename playwright/src/main/java/com/microsoft/playwright/{ => impl}/Listener.java (89%) diff --git a/playwright/src/main/java/com/microsoft/playwright/Browser.java b/playwright/src/main/java/com/microsoft/playwright/Browser.java index 67ad1b63..e572195e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Browser.java +++ b/playwright/src/main/java/com/microsoft/playwright/Browser.java @@ -48,12 +48,6 @@ public interface Browser { } } - enum EventType { - DISCONNECTED, - } - - void addListener(EventType type, Listener listener); - void removeListener(EventType type, Listener listener); void onDisconnected(Runnable handler); void offDisconnected(Runnable handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java index b85905fb..33934f1e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java +++ b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java @@ -94,13 +94,6 @@ public interface BrowserContext { } } - enum EventType { - CLOSE, - PAGE, - } - - void addListener(EventType type, Listener listener); - void removeListener(EventType type, Listener listener); void onClose(Runnable handler); void offClose(Runnable handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/Page.java b/playwright/src/main/java/com/microsoft/playwright/Page.java index 1a3733af..40441f25 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Page.java +++ b/playwright/src/main/java/com/microsoft/playwright/Page.java @@ -74,30 +74,6 @@ public interface Page { String stack(); } - enum EventType { - CLOSE, - CONSOLE, - CRASH, - DIALOG, - DOMCONTENTLOADED, - DOWNLOAD, - FILECHOOSER, - FRAMEATTACHED, - FRAMEDETACHED, - FRAMENAVIGATED, - LOAD, - PAGEERROR, - POPUP, - REQUEST, - REQUESTFAILED, - REQUESTFINISHED, - RESPONSE, - WEBSOCKET, - WORKER, - } - - void addListener(EventType type, Listener listener); - void removeListener(EventType type, Listener listener); void onClose(Runnable handler); void offClose(Runnable handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/WebSocket.java b/playwright/src/main/java/com/microsoft/playwright/WebSocket.java index 811f3b36..25f5155d 100644 --- a/playwright/src/main/java/com/microsoft/playwright/WebSocket.java +++ b/playwright/src/main/java/com/microsoft/playwright/WebSocket.java @@ -29,15 +29,6 @@ public interface WebSocket { String text(); } - enum EventType { - CLOSE, - FRAMERECEIVED, - FRAMESENT, - SOCKETERROR, - } - - void addListener(EventType type, Listener listener); - void removeListener(EventType type, Listener listener); void onClose(Runnable handler); void offClose(Runnable handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/Worker.java b/playwright/src/main/java/com/microsoft/playwright/Worker.java index 28e34cca..3b7710d1 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Worker.java +++ b/playwright/src/main/java/com/microsoft/playwright/Worker.java @@ -25,12 +25,6 @@ import java.util.function.Consumer; * worker is gone. */ public interface Worker { - enum EventType { - CLOSE, - } - - void addListener(EventType type, Listener listener); - void removeListener(EventType type, Listener listener); void onClose(Consumer handler); void offClose(Consumer handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java index 3d8129a1..6d2ffca8 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java @@ -21,7 +21,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.*; -import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; @@ -46,6 +45,11 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { final TimeoutSettings timeoutSettings = new TimeoutSettings(); Path videosDir; + enum EventType { + CLOSE, + PAGE, + } + BrowserContextImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); if (parent instanceof BrowserImpl) { @@ -55,16 +59,6 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { } } - @Override - public void addListener(EventType type, Listener listener) { - listeners.add(type, listener); - } - - @Override - public void removeListener(EventType type, Listener listener) { - listeners.remove(type, listener); - } - @Override public void onClose(Runnable handler) { listeners.add(EventType.CLOSE, handler); @@ -359,7 +353,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { private String errorMessage; WaitableContextClose() { - addListener(EventType.CLOSE, this); + listeners.add(EventType.CLOSE, this); } @Override @@ -384,7 +378,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { @Override public void dispose() { - removeListener(EventType.CLOSE, this); + listeners.remove(EventType.CLOSE, this); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java index 40076d3a..3db461de 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java @@ -37,20 +37,14 @@ class BrowserImpl extends ChannelOwner implements Browser { private final ListenerCollection listeners = new ListenerCollection<>(); private boolean isConnected = true; + enum EventType { + DISCONNECTED, + } + BrowserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); } - @Override - public void addListener(EventType type, Listener listener) { - listeners.add(type, listener); - } - - @Override - public void removeListener(EventType type, Listener listener) { - listeners.remove(type, listener); - } - @Override public void onDisconnected(Runnable handler) { listeners.add(EventType.DISCONNECTED, handler); diff --git a/playwright/src/main/java/com/microsoft/playwright/Listener.java b/playwright/src/main/java/com/microsoft/playwright/impl/Listener.java similarity index 89% rename from playwright/src/main/java/com/microsoft/playwright/Listener.java rename to playwright/src/main/java/com/microsoft/playwright/impl/Listener.java index 926f1cc1..b83c137c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Listener.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Listener.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package com.microsoft.playwright; +package com.microsoft.playwright.impl; + +import com.microsoft.playwright.Event; public interface Listener { void handle(Event event); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java b/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java index 5bb276a5..49c083de 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ListenerCollection.java @@ -17,7 +17,6 @@ package com.microsoft.playwright.impl; import com.microsoft.playwright.Event; -import com.microsoft.playwright.Listener; import java.util.*; import java.util.function.Consumer; @@ -89,7 +88,7 @@ class ListenerCollection { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RunnableWrapper that = (RunnableWrapper) o; + ConsumerWrapper that = (ConsumerWrapper) o; return Objects.equals(callback, that.callback); } 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 45c816c1..4ec6ed45 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -18,15 +18,12 @@ package com.microsoft.playwright.impl; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.google.gson.reflect.TypeToken; import com.microsoft.playwright.*; import java.nio.file.Path; import java.util.*; -import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Predicate; -import java.util.function.Supplier; import java.util.regex.Pattern; import static com.microsoft.playwright.impl.Serialization.gson; @@ -68,6 +65,28 @@ public class PageImpl extends ChannelOwner implements Page { private final TimeoutSettings timeoutSettings; private VideoImpl video; + enum EventType { + CLOSE, + CONSOLE, + CRASH, + DIALOG, + DOMCONTENTLOADED, + DOWNLOAD, + FILECHOOSER, + FRAMEATTACHED, + FRAMEDETACHED, + FRAMENAVIGATED, + LOAD, + PAGEERROR, + POPUP, + REQUEST, + REQUESTFAILED, + REQUESTFINISHED, + RESPONSE, + WEBSOCKET, + WORKER, + } + PageImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); browserContext = (BrowserContextImpl) parent; @@ -217,16 +236,6 @@ public class PageImpl extends ChannelOwner implements Page { sendMessage("setFileChooserInterceptedNoReply", params); } - @Override - public void addListener(EventType type, Listener listener) { - listeners.add(type, listener); - } - - @Override - public void removeListener(EventType type, Listener listener) { - listeners.remove(type, listener); - } - @Override public void onClose(Runnable handler) { listeners.add(EventType.CLOSE, handler); @@ -1207,7 +1216,7 @@ public class PageImpl extends ChannelOwner implements Page { WaitablePageClose() { subscribedEvents = Arrays.asList(EventType.CLOSE, EventType.CRASH); for (EventType e : subscribedEvents) { - addListener(e, this); + listeners.add(e, this); } } @@ -1236,7 +1245,7 @@ public class PageImpl extends ChannelOwner implements Page { @Override public void dispose() { for (EventType e : subscribedEvents) { - removeListener(e, this); + listeners.remove(e, this); } } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/WaitableEvent.java b/playwright/src/main/java/com/microsoft/playwright/impl/WaitableEvent.java index 4f21e1cf..ca2077eb 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/WaitableEvent.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/WaitableEvent.java @@ -17,8 +17,6 @@ package com.microsoft.playwright.impl; import com.microsoft.playwright.Event; -import com.microsoft.playwright.Listener; -import com.microsoft.playwright.Page; import java.util.function.Predicate; diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/WebSocketImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/WebSocketImpl.java index a1c84406..15715a61 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/WebSocketImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/WebSocketImpl.java @@ -31,21 +31,18 @@ class WebSocketImpl extends ChannelOwner implements WebSocket { private final PageImpl page; private boolean isClosed; - public WebSocketImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { + enum EventType { + CLOSE, + FRAMERECEIVED, + FRAMESENT, + SOCKETERROR, + } + + WebSocketImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); page = (PageImpl) parent; } - @Override - public void addListener(EventType type, Listener listener) { - listeners.add(type, listener); - } - - @Override - public void removeListener(EventType type, Listener listener) { - listeners.remove(type, listener); - } - @Override public void onClose(Runnable handler) { listeners.add(EventType.CLOSE, handler); @@ -127,7 +124,7 @@ class WebSocketImpl extends ChannelOwner implements WebSocket { WaitableWebSocketError() { subscribedEvents = Arrays.asList(EventType.CLOSE, EventType.SOCKETERROR); for (EventType e : subscribedEvents) { - addListener(e, this); + listeners.add(e, this); } } @@ -156,7 +153,7 @@ class WebSocketImpl extends ChannelOwner implements WebSocket { @Override public void dispose() { for (EventType e : subscribedEvents) { - removeListener(e, this); + listeners.remove(e, this); } } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java index b17ac90c..a526d609 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java @@ -32,20 +32,14 @@ class WorkerImpl extends ChannelOwner implements Worker { private final ListenerCollection listeners = new ListenerCollection<>(); PageImpl page; + enum EventType { + CLOSE, + } + WorkerImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); } - @Override - public void addListener(EventType type, Listener listener) { - listeners.add(type, listener); - } - - @Override - public void removeListener(EventType type, Listener listener) { - listeners.remove(type, listener); - } - @Override public void onClose(Consumer handler) { listeners.add(EventType.CLOSE, handler); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBase.java b/playwright/src/test/java/com/microsoft/playwright/TestBase.java index f1455a21..d489c821 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBase.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBase.java @@ -16,7 +16,10 @@ package com.microsoft.playwright; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import java.io.IOException; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java index af3718a5..882f138c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java @@ -22,8 +22,6 @@ import org.junit.jupiter.api.Test; import java.io.OutputStreamWriter; import java.util.List; -import static com.microsoft.playwright.BrowserContext.EventType.PAGE; -import static com.microsoft.playwright.Page.EventType.POPUP; import static org.junit.jupiter.api.Assertions.*; public class TestBrowserContextBasic extends TestBase { @@ -181,7 +179,7 @@ public class TestBrowserContextBasic extends TestBase { } }); Page[] popup = {null}; - context.addListener(PAGE, event -> popup[0] = (Page) event.data()); + context.onPage(page1 -> popup[0] = page1); page.navigate(server.EMPTY_PAGE); page.click("'Click me'"); context.close(); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java index bc34663b..4e1504c8 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCredentials.java @@ -19,7 +19,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; -import static com.microsoft.playwright.Utils.getOS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextRoute.java index 3648913a..8e06af1d 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextRoute.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -import java.util.function.BiConsumer; import java.util.function.Consumer; import static java.util.Arrays.asList; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestClick.java b/playwright/src/test/java/com/microsoft/playwright/TestClick.java index fcb66b82..9272b22d 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestClick.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestClick.java @@ -23,11 +23,9 @@ import org.junit.jupiter.api.condition.EnabledIf; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.function.Consumer; import static com.microsoft.playwright.Keyboard.Modifier.SHIFT; import static com.microsoft.playwright.Mouse.Button.RIGHT; -import static com.microsoft.playwright.Page.EventType.CONSOLE; import static org.junit.jupiter.api.Assertions.*; public class TestClick extends TestBase { @@ -141,7 +139,7 @@ public class TestClick extends TestBase { void shouldClickOffscreenButtons() { page.navigate(server.PREFIX + "/offscreenbuttons.html"); List messages = new ArrayList<>(); - page.addListener(CONSOLE, event -> messages.add(((ConsoleMessage) event.data()).text())); + page.onConsole(message -> messages.add(message.text())); for (int i = 0; i < 11; ++i) { // We might've scrolled to click a button - reset to (0, 0). page.evaluate("() => window.scrollTo(0, 0)"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestDefaultBrowserContext2.java b/playwright/src/test/java/com/microsoft/playwright/TestDefaultBrowserContext2.java index 626f0817..5a79b02d 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestDefaultBrowserContext2.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestDefaultBrowserContext2.java @@ -223,7 +223,7 @@ public class TestDefaultBrowserContext2 extends TestBase { void shouldFireCloseEventForAPersistentContext() { launchPersistent(); boolean[] closed = {false}; - persistentContext.addListener(BrowserContext.EventType.CLOSE, event -> closed[0] = true); + persistentContext.onClose(() -> closed[0] = true); closePersistentContext(); assertTrue(closed[0]); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestDialog.java b/playwright/src/test/java/com/microsoft/playwright/TestDialog.java index 8f4a02d1..6e4681d2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestDialog.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestDialog.java @@ -18,11 +18,9 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; -import org.junit.jupiter.api.condition.EnabledIf; import static com.microsoft.playwright.Dialog.Type.ALERT; import static com.microsoft.playwright.Dialog.Type.PROMPT; -import static com.microsoft.playwright.Page.EventType.DIALOG; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -30,8 +28,7 @@ public class TestDialog extends TestBase { @Test void shouldFire() { - page.addListener(DIALOG, event -> { - Dialog dialog = (Dialog) event.data(); + page.onDialog(dialog -> { assertEquals(ALERT, dialog.type()); assertEquals( "", dialog.defaultValue()); assertEquals( "yo", dialog.message()); @@ -42,8 +39,7 @@ public class TestDialog extends TestBase { @Test void shouldAllowAcceptingPrompts() { - page.addListener(DIALOG, event -> { - Dialog dialog = (Dialog) event.data(); + page.onDialog(dialog -> { assertEquals(PROMPT, dialog.type()); assertEquals("yes.", dialog.defaultValue()); assertEquals("question?", dialog.message()); @@ -55,8 +51,7 @@ public class TestDialog extends TestBase { @Test void shouldDismissThePrompt() { - page.addListener(DIALOG, event -> { - Dialog dialog = (Dialog) event.data(); + page.onDialog(dialog -> { dialog.dismiss(); }); Object result = page.evaluate("() => prompt('question?')"); @@ -65,8 +60,7 @@ public class TestDialog extends TestBase { @Test void shouldAcceptTheConfirmPrompt() { - page.addListener(DIALOG, event -> { - Dialog dialog = (Dialog) event.data(); + page.onDialog(dialog -> { dialog.accept(); }); Object result = page.evaluate("() => confirm('boolean?')"); @@ -75,8 +69,7 @@ public class TestDialog extends TestBase { @Test void shouldDismissTheConfirmPrompt() { - page.addListener(DIALOG, event -> { - Dialog dialog = (Dialog) event.data(); + page.onDialog(dialog -> { dialog.dismiss(); }); Object result = page.evaluate("() => confirm('boolean?')"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestDispatchEvent.java b/playwright/src/test/java/com/microsoft/playwright/TestDispatchEvent.java index 9ef4fe9b..57781da7 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestDispatchEvent.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestDispatchEvent.java @@ -19,7 +19,8 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import static com.microsoft.playwright.Utils.mapOf; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class TestDispatchEvent extends TestBase { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestDownload.java b/playwright/src/test/java/com/microsoft/playwright/TestDownload.java index 6c69661e..d2e50c41 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestDownload.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestDownload.java @@ -21,8 +21,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; import org.junit.jupiter.api.condition.EnabledIf; -import java.io.*; -import java.nio.charset.StandardCharsets; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -30,7 +32,6 @@ import java.time.Duration; import java.time.Instant; import static com.microsoft.playwright.Keyboard.Modifier.ALT; -import static com.microsoft.playwright.Page.EventType.DOWNLOAD; import static com.microsoft.playwright.Utils.copy; import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.file.Files.readAllBytes; @@ -249,16 +250,15 @@ public class TestDownload extends TestBase { Page page = browser.newPage(new Browser.NewPageOptions().withAcceptDownloads(true)); page.setContent("download"); @SuppressWarnings("unchecked") - Event[] event = new Event[]{null}; - page.addListener(DOWNLOAD, e -> event[0] = e); + Download[] download = {null}; + page.onDownload(d -> download[0] = d); page.click("a"); Instant start = Instant.now(); - while (event[0] == null) { + while (download[0] == null) { page.waitForTimeout(100); assertTrue(Duration.between(start, Instant.now()).getSeconds() < 30, "Timed out"); } - Download download = (Download) event[0].data(); - Path path = download.path(); + Path path = download[0].path(); assertTrue(Files.exists(path)); byte[] bytes = readAllBytes(path); assertEquals("Hello world", new String(bytes, UTF_8)); @@ -269,17 +269,16 @@ public class TestDownload extends TestBase { void shouldReportDownloadPathWithinPageOnDownloadHandlerForBlobs() throws IOException { Page page = browser.newPage(new Browser.NewPageOptions().withAcceptDownloads(true)); @SuppressWarnings("unchecked") - Event[] event = new Event[]{null}; - page.addListener(DOWNLOAD, e -> event[0] = e); + Download[] download = {null}; + page.onDownload(d -> download[0] = d); page.navigate(server.PREFIX + "/download-blob.html"); page.click("a"); Instant start = Instant.now(); - while (event[0] == null) { + while (download[0] == null) { page.waitForTimeout(100); assertTrue(Duration.between(start, Instant.now()).getSeconds() < 1, "Timed out"); } - Download download = (Download) event[0].data(); - Path path = download.path(); + Path path = download[0].path(); assertTrue(Files.exists(path)); byte[] bytes = readAllBytes(path); assertEquals("Hello world", new String(bytes, UTF_8)); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleSelectText.java b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleSelectText.java index 05691edb..a816f444 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleSelectText.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleSelectText.java @@ -2,9 +2,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicBoolean; - import static org.junit.jupiter.api.Assertions.*; public class TestElementHandleSelectText extends TestBase { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestGeolocation.java b/playwright/src/test/java/com/microsoft/playwright/TestGeolocation.java index fd4a6807..7f52f854 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestGeolocation.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestGeolocation.java @@ -21,8 +21,6 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -import static com.microsoft.playwright.Page.EventType.CONSOLE; -import static com.microsoft.playwright.Page.EventType.POPUP; import static com.microsoft.playwright.Utils.mapOf; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -111,7 +109,7 @@ public class TestGeolocation extends TestBase { context.grantPermissions(asList("geolocation")); page.navigate(server.EMPTY_PAGE); List messages = new ArrayList<>(); - page.addListener(CONSOLE, event -> messages.add(((ConsoleMessage) event.data()).text())); + page.onConsole(message -> messages.add(message.text())); context.setGeolocation(new Geolocation()); page.evaluate("() => {\n" + " navigator.geolocation.watchPosition(pos => {\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/TestHar.java b/playwright/src/test/java/com/microsoft/playwright/TestHar.java index b9562dc4..2105310f 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestHar.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestHar.java @@ -19,12 +19,10 @@ package com.microsoft.playwright; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.stream.JsonReader; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestNetworkRequest.java b/playwright/src/test/java/com/microsoft/playwright/TestNetworkRequest.java index 937dd0ab..7f92f9bf 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestNetworkRequest.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestNetworkRequest.java @@ -28,8 +28,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.stream.Collectors; -import static com.microsoft.playwright.Page.EventType.REQUEST; -import static com.microsoft.playwright.Page.EventType.RESPONSE; import static com.microsoft.playwright.Utils.attachFrame; import static com.microsoft.playwright.Utils.mapOf; import static org.junit.jupiter.api.Assertions.*; @@ -38,7 +36,7 @@ public class TestNetworkRequest extends TestBase { @Test void shouldWorkForMainFrameNavigationRequest() { List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); page.navigate(server.EMPTY_PAGE); assertEquals(1, requests.size()); assertEquals(page.mainFrame(), requests.get(0).frame()); @@ -48,7 +46,7 @@ public class TestNetworkRequest extends TestBase { void shouldWorkForSubframeNavigationRequest() { page.navigate(server.EMPTY_PAGE); List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); attachFrame(page, "frame1", server.EMPTY_PAGE); assertEquals(1, requests.size()); assertEquals(page.frames().get(1), requests.get(0).frame()); @@ -58,7 +56,7 @@ public class TestNetworkRequest extends TestBase { void shouldWorkForFetchRequests() { page.navigate(server.EMPTY_PAGE); List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); page.evaluate("() => fetch('/digits/1.png')"); assertEquals(1, requests.size()); assertEquals(page.mainFrame(), requests.get(0).frame()); @@ -68,7 +66,7 @@ public class TestNetworkRequest extends TestBase { void shouldWorkForARedirect() { server.setRedirect("/foo.html", "/empty.html"); List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); page.navigate(server.PREFIX + "/foo.html"); assertEquals(2, requests.size()); @@ -152,7 +150,7 @@ public class TestNetworkRequest extends TestBase { exchange.getResponseBody().close(); }); Request[] request = {null}; - page.addListener(REQUEST, event -> request[0] = (Request) event.data()); + page.onRequest(r -> request[0] = r); page.evaluate("() => fetch('./post', { method: 'POST', body: JSON.stringify({foo: 'bar'})})"); assertNotNull(request[0]); assertEquals("{\"foo\":\"bar\"}", request[0].postData()); @@ -166,7 +164,7 @@ public class TestNetworkRequest extends TestBase { exchange.getResponseBody().close(); }); Request[] request = {null}; - page.addListener(REQUEST, event -> request[0] = (Request) event.data()); + page.onRequest(r -> request[0] = r); page.evaluate("async () => {\n" + " await fetch('./post', { method: 'POST', body: new Uint8Array(Array.from(Array(256).keys())) });\n" + "}"); @@ -186,7 +184,7 @@ public class TestNetworkRequest extends TestBase { exchange.getResponseBody().close(); }); Request[] request = {null}; - page.addListener(REQUEST, event -> request[0] = (Request) event.data()); + page.onRequest(r -> request[0] = r); page.route("/post", route -> route.continue_()); page.evaluate("async () => {\n" + " await fetch('./post', { method: 'POST', body: new Uint8Array(Array.from(Array(256).keys())) });\n" + @@ -228,7 +226,7 @@ public class TestNetworkRequest extends TestBase { // 2. Subscribe to page request events. page.navigate(server.EMPTY_PAGE); List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); // 3. Connect to EventSource in browser and return first message. Object result = page.evaluate("() => {\n" + " const eventSource = new EventSource('/sse');\n" + @@ -243,8 +241,7 @@ public class TestNetworkRequest extends TestBase { @Test void shouldReturnNavigationBit() { Map requests = new HashMap<>(); - page.addListener(REQUEST, event -> { - Request request = (Request) event.data(); + page.onRequest(request -> { String name = request.url(); int lastSlash = name.lastIndexOf('/'); if (lastSlash != -1) { @@ -264,7 +261,7 @@ public class TestNetworkRequest extends TestBase { @Test void shouldReturnNavigationBitWhenNavigatingToImage() { List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); page.navigate(server.PREFIX + "/pptr.png"); assertTrue(requests.get(0).isNavigationRequest()); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestNetworkResponse.java b/playwright/src/test/java/com/microsoft/playwright/TestNetworkResponse.java index 8d6cdd84..3873d60c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestNetworkResponse.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestNetworkResponse.java @@ -24,12 +24,8 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import java.util.concurrent.Semaphore; -import java.util.regex.Pattern; -import static com.microsoft.playwright.Page.EventType.REQUESTFINISHED; -import static com.microsoft.playwright.Page.EventType.RESPONSE; import static org.junit.jupiter.api.Assertions.*; public class TestNetworkResponse extends TestBase { @@ -102,8 +98,8 @@ public class TestNetworkResponse extends TestBase { }); // Setup page to trap response. boolean[] requestFinished = {false}; - page.addListener(REQUESTFINISHED, event -> { - requestFinished[0] |= ((Request) event.data()).url().contains("/get"); + page.onRequestFinished(request -> { + requestFinished[0] |= request.url().contains("/get"); }); // send request and wait for server response Response pageResponse = page.waitForResponse(() -> page.evaluate("() => fetch('./get', { method: 'GET'})")); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java index 520addde..2cf5fae4 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; -import static com.microsoft.playwright.Page.EventType.*; import static com.microsoft.playwright.Page.LoadState.DOMCONTENTLOADED; import static com.microsoft.playwright.Page.LoadState.LOAD; import static org.junit.jupiter.api.Assertions.*; @@ -91,7 +90,7 @@ public class TestPageBasic extends TestBase { // fire. newPage.click("body"); boolean[] didShowDialog = {false}; - newPage.addListener(DIALOG, event -> didShowDialog[0] = true); + newPage.onDialog(dialog -> didShowDialog[0] = true); newPage.close(); assertFalse(didShowDialog[0]); } @@ -265,7 +264,7 @@ public class TestPageBasic extends TestBase { void pagePressShouldWorkForEnter() { page.setContent(""); List messages = new ArrayList<>(); - page.addListener(CONSOLE, event -> messages.add((ConsoleMessage) event.data())); + page.onConsole(message -> messages.add(message)); page.press("input", "Enter"); assertEquals("press", messages.get(0).text()); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageEmulateMedia.java b/playwright/src/test/java/com/microsoft/playwright/TestPageEmulateMedia.java index e495f17b..d8c1fd3b 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageEmulateMedia.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageEmulateMedia.java @@ -23,7 +23,6 @@ import java.util.function.Supplier; import static com.microsoft.playwright.ColorScheme.DARK; import static com.microsoft.playwright.ColorScheme.LIGHT; import static com.microsoft.playwright.Page.EmulateMediaParams.Media.PRINT; -import static com.microsoft.playwright.Page.EventType.POPUP; import static com.microsoft.playwright.Utils.attachFrame; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java b/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java index fb3671c6..c6e8d31d 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java @@ -18,11 +18,9 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; -import org.junit.jupiter.api.condition.EnabledIf; import java.util.Map; -import static com.microsoft.playwright.Page.EventType.FRAMENAVIGATED; import static com.microsoft.playwright.Utils.mapOf; import static java.util.Arrays.asList; import static java.util.Collections.emptyMap; @@ -174,8 +172,7 @@ public class TestPageEvaluate extends TestBase { @Test void shouldWorkRightAfterFramenavigated() { Object[] frameEvaluation = {null}; - page.addListener(FRAMENAVIGATED, event -> { - Frame frame = (Frame) event.data(); + page.onFrameNavigated(frame -> { frameEvaluation[0] = frame.evaluate("() => 6 * 7"); }); page.navigate(server.EMPTY_PAGE); @@ -186,8 +183,7 @@ public class TestPageEvaluate extends TestBase { void shouldWorkRightAfterACrossOriginNavigation() { page.navigate(server.EMPTY_PAGE); Object[] frameEvaluation = {null}; - page.addListener(FRAMENAVIGATED, event -> { - Frame frame = (Frame) event.data(); + page.onFrameNavigated(frame -> { frameEvaluation[0] = frame.evaluate("() => 6 * 7"); }); page.navigate(server.CROSS_PROCESS_PREFIX + "/empty.html"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageEventNetwork.java b/playwright/src/test/java/com/microsoft/playwright/TestPageEventNetwork.java index 7a620da4..9bcca040 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageEventNetwork.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageEventNetwork.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; -import static com.microsoft.playwright.Page.EventType.*; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -29,7 +28,7 @@ public class TestPageEventNetwork extends TestBase { @Test void PageEventsRequest() { List requests = new ArrayList<>(); - page.addListener(REQUEST, event -> requests.add((Request) event.data())); + page.onRequest(request -> requests.add(request)); page.navigate(server.EMPTY_PAGE); assertEquals(1, requests.size()); assertEquals(server.EMPTY_PAGE, requests.get(0).url()); @@ -43,7 +42,7 @@ public class TestPageEventNetwork extends TestBase { @Test void PageEventsResponse() { List responses = new ArrayList<>(); - page.addListener(RESPONSE, event -> responses.add((Response) event.data())); + page.onResponse(response -> responses.add(response)); page.navigate(server.EMPTY_PAGE); assertEquals(1, responses.size()); assertEquals(server.EMPTY_PAGE, responses.get(0).url()); @@ -56,7 +55,7 @@ public class TestPageEventNetwork extends TestBase { void PageEventsRequestFailed() { server.setRoute("/one-style.css", exchange -> exchange.getResponseBody().close()); List failedRequests = new ArrayList<>(); - page.addListener(REQUESTFAILED, event -> failedRequests.add((Request) event.data())); + page.onRequestFailed(request -> failedRequests.add(request)); page.navigate(server.PREFIX + "/one-style.html"); assertEquals(1, failedRequests.size()); assertTrue(failedRequests.get(0).url().contains("one-style.css")); @@ -96,8 +95,8 @@ public class TestPageEventNetwork extends TestBase { @Test void shouldFireEventsInProperOrder() { List events = new ArrayList<>(); - page.addListener(REQUEST, event -> events.add("request")); - page.addListener(RESPONSE, event -> events.add("response")); + page.onRequest(request -> events.add("request")); + page.onResponse(response -> events.add("response")); Response response = page.navigate(server.EMPTY_PAGE); assertNull(response.finished()); events.add("requestfinished"); @@ -107,20 +106,16 @@ public class TestPageEventNetwork extends TestBase { @Test void shouldSupportRedirects() { List events = new ArrayList<>(); - page.addListener(REQUEST, event -> { - Request request = (Request) event.data(); + page.onRequest(request -> { events.add(request.method() + " " + request.url()); }); - page.addListener(RESPONSE,event -> { - Response response = (Response) event.data(); + page.onResponse(response -> { events.add(response.status() + " " + response.url()); }); - page.addListener(REQUESTFINISHED, event -> { - Request request = (Request) event.data(); + page.onRequestFinished(request -> { events.add("DONE " + request.url()); }); - page.addListener(REQUESTFAILED, event -> { - Request request = (Request) event.data(); + page.onRequestFailed(request -> { events.add("FAIL " + request.url()); }); server.setRedirect("/foo.html", "/empty.html"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index afc7aaf3..9db3b953 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -18,18 +18,18 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; -import org.junit.jupiter.api.condition.EnabledIf; import java.io.OutputStreamWriter; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.regex.Pattern; -import static com.microsoft.playwright.Page.EventType.REQUEST; -import static com.microsoft.playwright.Page.EventType.REQUESTFAILED; import static com.microsoft.playwright.Utils.mapOf; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -198,8 +198,7 @@ public class TestPageRoute extends TestBase { void shouldBeAbortable() { page.route(Pattern.compile(".*\\.css$"), route -> route.abort()); boolean[] failed = {false}; - page.addListener(REQUESTFAILED, event -> { - Request request = (Request) event.data(); + page.onRequestFailed(request -> { if (request.url().contains(".css")) failed[0] = true; }); @@ -212,19 +211,19 @@ public class TestPageRoute extends TestBase { @Test void shouldBeAbortableWithCustomErrorCodes() { page.route("**/*", route -> route.abort("internetdisconnected")); - Request[] failedRequest = {null}; - page.addListener(REQUESTFAILED, event -> failedRequest[0] = (Request) event.data()); - try { - page.navigate(server.EMPTY_PAGE); - } catch (PlaywrightException e) { - } - assertNotNull(failedRequest[0]); + Request failedRequest = page.waitForRequestFailed(() -> { + try { + page.navigate(server.EMPTY_PAGE); + } catch (PlaywrightException e) { + } + }); + assertNotNull(failedRequest); if (isWebKit()) - assertEquals("Request intercepted", failedRequest[0].failure().errorText()); + assertEquals("Request intercepted", failedRequest.failure().errorText()); else if (isFirefox()) - assertEquals("NS_ERROR_OFFLINE", failedRequest[0].failure().errorText()); + assertEquals("NS_ERROR_OFFLINE", failedRequest.failure().errorText()); else - assertEquals("net::ERR_INTERNET_DISCONNECTED", failedRequest[0].failure().errorText()); + assertEquals("net::ERR_INTERNET_DISCONNECTED", failedRequest.failure().errorText()); } @Test diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageSelectOption.java b/playwright/src/test/java/com/microsoft/playwright/TestPageSelectOption.java index 757ca479..29f754c2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageSelectOption.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageSelectOption.java @@ -18,7 +18,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; -import java.util.Arrays; import java.util.Collections; import java.util.List; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java b/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java index a101e877..ba7f669d 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java @@ -18,7 +18,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; -import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.time.Duration; @@ -26,6 +25,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; +import java.util.function.Consumer; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -79,11 +79,11 @@ public class TestPageSetInputFiles extends TestBase { void shouldEmitEventAddListenerRemoveListener() { page.setContent(""); FileChooser[] chooser = { null }; - page.addListener(Page.EventType.FILECHOOSER, new Listener() { + page.onFileChooser(new Consumer() { @Override - public void handle(Event event) { - chooser[0] = (FileChooser) event.data(); - page.removeListener(Page.EventType.FILECHOOSER, this); + public void accept(FileChooser fileChooser) { + chooser[0] = fileChooser; + page.offFileChooser(this); } }); page.click("input"); @@ -198,8 +198,7 @@ public class TestPageSetInputFiles extends TestBase { @Test void shouldBeAbleToReadSelectedFile() { page.setContent(""); - page.addListener(Page.EventType.FILECHOOSER, event -> { - FileChooser fileChooser = (FileChooser) event.data(); + page.onFileChooser(fileChooser -> { fileChooser.setFiles(FILE_TO_UPLOAD); }); Object content = page.evalOnSelector("input", "async picker => {\n" + @@ -216,12 +215,11 @@ public class TestPageSetInputFiles extends TestBase { @Test void shouldBeAbleToResetSelectedFilesWithEmptyFileList() { page.setContent(""); - page.addListener(Page.EventType.FILECHOOSER, new Listener() { + page.onFileChooser(new Consumer() { @Override - public void handle(Event event) { - FileChooser fileChooser = (FileChooser) event.data(); + public void accept(FileChooser fileChooser) { fileChooser.setFiles(FILE_TO_UPLOAD); - page.removeListener(Page.EventType.FILECHOOSER, this); + page.offFileChooser(this); } }); Object fileLength1 = page.evalOnSelector("input", "async picker => {\n" + @@ -231,12 +229,11 @@ public class TestPageSetInputFiles extends TestBase { "}"); assertEquals(1, fileLength1); - page.addListener(Page.EventType.FILECHOOSER, new Listener() { + page.onFileChooser(new Consumer() { @Override - public void handle(Event event) { - FileChooser fileChooser = (FileChooser) event.data(); + public void accept(FileChooser fileChooser) { fileChooser.setFiles(new Path[0]); - page.removeListener(Page.EventType.FILECHOOSER, this); + page.offFileChooser(this); } }); Object fileLength2 = page.evalOnSelector("input", "async picker => {\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java index 22c3d8e8..be8456f7 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java @@ -20,12 +20,10 @@ import org.junit.jupiter.api.Test; import java.net.MalformedURLException; import java.net.URL; -import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; -import static com.microsoft.playwright.Page.EventType.FRAMENAVIGATED; -import static com.microsoft.playwright.Utils.*; +import static com.microsoft.playwright.Utils.expectedSSLError; import static org.junit.jupiter.api.Assertions.*; public class TestPageWaitForNavigation extends TestBase { @@ -135,10 +133,9 @@ public class TestPageWaitForNavigation extends TestBase { void shouldWorkWhenSubframeIssuesWindowStop() { server.setRoute("/frames/style.css", exchange -> {}); boolean[] frameWindowStopCalled = {false}; - page.addListener(Page.EventType.FRAMEATTACHED, event -> { - Frame frame = (Frame) event.data(); - page.addListener(FRAMENAVIGATED, event1 -> { - if (frame.equals(event1.data())) { + page.onFrameAttached(frame -> { + page.onFrameNavigated(frame1 -> { + if (frame.equals(frame1)) { frame.evaluate("window.stop()"); frameWindowStopCalled[0] = true; } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java index 2c57f0a8..0a125ca4 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForRequest.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import java.util.regex.Pattern; -import static com.microsoft.playwright.Page.EventType.REQUEST; import static org.junit.jupiter.api.Assertions.*; public class TestPageWaitForRequest extends TestBase { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java index e399b3e4..04001bb7 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForResponse.java @@ -18,8 +18,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; -import static com.microsoft.playwright.Page.EventType.REQUEST; -import static com.microsoft.playwright.Page.EventType.RESPONSE; import static org.junit.jupiter.api.Assertions.*; public class TestPageWaitForResponse extends TestBase { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPdf.java b/playwright/src/test/java/com/microsoft/playwright/TestPdf.java index 91f43386..6ec01840 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPdf.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPdf.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.condition.DisabledIf; import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.io.TempDir; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPopup.java b/playwright/src/test/java/com/microsoft/playwright/TestPopup.java index a88d801c..38ce656a 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPopup.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPopup.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import static com.microsoft.playwright.Page.EventType.POPUP; import static com.microsoft.playwright.Page.LoadState.DOMCONTENTLOADED; import static com.microsoft.playwright.Utils.mapOf; import static org.junit.jupiter.api.Assertions.*; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java index 35b95f7d..663f4f91 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import static com.microsoft.playwright.Page.EventType.RESPONSE; import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.*; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java b/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java index 5f402b81..bb619209 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java @@ -19,7 +19,6 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestSelectorsRegister.java b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsRegister.java index caf57d06..49eb4362 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestSelectorsRegister.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsRegister.java @@ -16,7 +16,6 @@ package com.microsoft.playwright; -import com.microsoft.playwright.impl.ElementHandleImpl; import org.junit.jupiter.api.Test; import java.nio.file.Paths; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestTap.java b/playwright/src/test/java/com/microsoft/playwright/TestTap.java index ef2c1041..fa92e702 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestTap.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestTap.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.Semaphore; -import java.util.concurrent.atomic.AtomicBoolean; import static com.microsoft.playwright.Utils.mapOf; import static java.util.Arrays.asList; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java b/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java index 53542832..7a33b0f2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java @@ -23,7 +23,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import static com.microsoft.playwright.Page.EventType.CONSOLE; import static com.microsoft.playwright.Utils.mapOf; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -73,7 +72,7 @@ public class TestWaitForFunction extends TestBase { @Test void shouldAvoidSideEffectsAfterTimeout() { int[] counter = { 0 }; - page.addListener(CONSOLE, event -> ++counter[0]); + page.onConsole(message -> ++counter[0]); try { JSHandle result = page.waitForFunction("() => {\n" + @@ -239,8 +238,7 @@ public class TestWaitForFunction extends TestBase { void shouldNotBeCalledAfterFinishingSuccessfully() { page.navigate(server.EMPTY_PAGE); List messages = new ArrayList<>(); - page.addListener(CONSOLE, event -> { - ConsoleMessage msg = (ConsoleMessage) event.data(); + page.onConsole(msg -> { if (msg.text().startsWith("waitForFunction")) { messages.add(msg.text()); } @@ -266,10 +264,10 @@ public class TestWaitForFunction extends TestBase { void shouldNotBeCalledAfterFinishingUnsuccessfully() { page.navigate(server.EMPTY_PAGE); List messages = new ArrayList<>(); - page.addListener(CONSOLE, event -> { - ConsoleMessage msg = (ConsoleMessage) event.data(); - if (msg.text().startsWith("waitForFunction")) + page.onConsole(msg -> { + if (msg.text().startsWith("waitForFunction")) { messages.add(msg.text()); + } }); try { page.waitForFunction("() => {\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/TestWebSocket.java b/playwright/src/test/java/com/microsoft/playwright/TestWebSocket.java index c95b9ca8..02811bae 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestWebSocket.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestWebSocket.java @@ -17,11 +17,7 @@ package com.microsoft.playwright; import org.java_websocket.WebSocket; -import org.java_websocket.drafts.Draft; -import org.java_websocket.exceptions.InvalidDataException; -import org.java_websocket.framing.Framedata; import org.java_websocket.handshake.ClientHandshake; -import org.java_websocket.handshake.ServerHandshakeBuilder; import org.java_websocket.server.WebSocketServer; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -29,14 +25,11 @@ import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.InetSocketAddress; -import java.nio.channels.SelectionKey; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; -import static com.microsoft.playwright.Page.EventType.WEBSOCKET; -import static com.microsoft.playwright.WebSocket.EventType.*; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -105,11 +98,10 @@ public class TestWebSocket extends TestBase { boolean[] socketClosed = {false}; List log = new ArrayList<>(); com.microsoft.playwright.WebSocket[] webSocket = {null}; - page.addListener(WEBSOCKET, event -> { - com.microsoft.playwright.WebSocket ws = (com.microsoft.playwright.WebSocket) event.data(); + page.onWebSocket(ws -> { log.add("open<" + ws.url() + ">"); webSocket[0] = ws; - ws.addListener(com.microsoft.playwright.WebSocket.EventType.CLOSE, closeEvent -> { + ws.onClose(() -> { log.add("close"); socketClosed[0] = true; }); @@ -127,12 +119,11 @@ public class TestWebSocket extends TestBase { void shouldEmitFrameEvents() { boolean[] socketClosed = {false}; List log = new ArrayList<>(); - page.addListener(WEBSOCKET, event -> { - com.microsoft.playwright.WebSocket ws = (com.microsoft.playwright.WebSocket) event.data(); + page.onWebSocket(ws -> { log.add("open"); - ws.addListener(FRAMESENT, e -> log.add("sent<" + ((com.microsoft.playwright.WebSocket.FrameData) e.data()).text() + ">")); - ws.addListener(FRAMERECEIVED, e -> log.add("received<" + ((com.microsoft.playwright.WebSocket.FrameData) e.data()).text() + ">")); - ws.addListener(CLOSE, e -> { log.add("close"); socketClosed[0] = true; }); + ws.onFrameSent(frameData -> log.add("sent<" + frameData.text() + ">")); + ws.onFrameReceived(frameData -> log.add("received<" + frameData.text() + ">")); + ws.onClose(() -> { log.add("close"); socketClosed[0] = true; }); }); page.evaluate("port => {\n" + " const ws = new WebSocket('ws://localhost:' + port + '/ws');\n" + @@ -154,10 +145,9 @@ public class TestWebSocket extends TestBase { void shouldEmitBinaryFrameEvents() { boolean[] socketClosed = {false}; List sent = new ArrayList<>(); - page.addListener(WEBSOCKET, event -> { - com.microsoft.playwright.WebSocket ws = (com.microsoft.playwright.WebSocket) event.data(); - ws.addListener(CLOSE, e -> { socketClosed[0] = true; }); - ws.addListener(FRAMESENT, e -> sent.add((com.microsoft.playwright.WebSocket.FrameData) e.data())); + page.onWebSocket(ws -> { + ws.onClose(() -> { socketClosed[0] = true; }); + ws.onFrameSent(frameData -> sent.add(frameData)); }); page.evaluate("port => {\n" + " const ws = new WebSocket('ws://localhost:' + port + '/ws');\n" + @@ -181,10 +171,9 @@ public class TestWebSocket extends TestBase { void shouldEmitError() { boolean[] socketError = {false}; String[] error = {null}; - page.addListener(WEBSOCKET, event -> { - com.microsoft.playwright.WebSocket ws = (com.microsoft.playwright.WebSocket) event.data(); - ws.addListener(SOCKETERROR, e -> { - error[0] = (String) e.data(); + page.onWebSocket(ws -> { + ws.onSocketError(e -> { + error[0] = e; socketError[0] = true; }); }); @@ -207,7 +196,7 @@ public class TestWebSocket extends TestBase { "}", webSocketServer.getPort()); }); boolean[] error = {false}; - ws.addListener(SOCKETERROR, e -> error[0] = true); + ws.onSocketError(e -> error[0] = true); ws.waitForFrameReceived(() -> {}); page.evaluate("window.ws.close()"); assertFalse(error[0]); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java b/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java index 6332b097..a5924629 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; import org.junit.jupiter.api.condition.EnabledIf; -import static com.microsoft.playwright.Page.EventType.*; import static com.microsoft.playwright.Utils.attachFrame; import static org.junit.jupiter.api.Assertions.*; @@ -110,7 +109,7 @@ public class TestWorkers extends TestBase { "() => new Worker(URL.createObjectURL(new Blob(['console.log(1)'], {type: 'application/javascript'})))")); assertEquals(1, page.workers().size()); boolean[] destroyed = {false}; - worker.addListener(Worker.EventType.CLOSE, event -> destroyed[0] = true); + worker.onClose(worker1 -> destroyed[0] = true); page.navigate(server.CROSS_PROCESS_PREFIX + "/empty.html"); assertTrue(destroyed[0]); assertEquals(0, page.workers().size()); diff --git a/playwright/src/test/java/com/microsoft/playwright/Utils.java b/playwright/src/test/java/com/microsoft/playwright/Utils.java index 26438fea..3afbceb5 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Utils.java +++ b/playwright/src/test/java/com/microsoft/playwright/Utils.java @@ -23,7 +23,6 @@ import com.google.gson.JsonParser; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashMap; import java.util.List; 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 e3e5ac91..31d76ca4 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 @@ -1075,16 +1075,6 @@ class Interface extends TypeDefinition { if (events.isEmpty()) { return; } - output.add(offset + "enum EventType {"); - for (int i = 0; i < events.size(); i++) { - String comma = i == events.size() ? "" : ","; - output.add(offset + " " + events.get(i).jsonName.toUpperCase() + comma); - } - output.add(offset + "}"); - output.add(""); - output.add(offset + "void addListener(EventType type, Listener listener);"); - output.add(offset + "void removeListener(EventType type, Listener listener);"); - for (Event e : events) { output.add(""); e.writeListenerMethods(output, offset);