From ea51452ed3cda735aa5f08b5c010153e87703ce8 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Fri, 3 Nov 2023 16:57:00 +0530 Subject: [PATCH] [bidi][java] Add realm related events --- java/src/org/openqa/selenium/bidi/Script.java | 36 +++++++++++++++ .../bidi/script/ScriptEventsTest.java | 46 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/Script.java b/java/src/org/openqa/selenium/bidi/Script.java index d08fb2b6a69ea..e8d91031677f0 100644 --- a/java/src/org/openqa/selenium/bidi/Script.java +++ b/java/src/org/openqa/selenium/bidi/Script.java @@ -74,6 +74,26 @@ public class Script implements Closeable { } }); + private final Event realmCreated = + new Event<>( + "script.realmCreated", + params -> { + try (StringReader reader = new StringReader(JSON.toJson(params)); + JsonInput input = JSON.newInput(reader)) { + return input.read(RealmInfo.class); + } + }); + + private final Event realmDestroyed = + new Event<>( + "script.realmDestroyed", + params -> { + try (StringReader reader = new StringReader(JSON.toJson(params)); + JsonInput input = JSON.newInput(reader)) { + return input.read(RealmInfo.class); + } + }); + public Script(WebDriver driver) { this(new HashSet<>(), driver); } @@ -314,6 +334,22 @@ public void onMessage(Consumer consumer) { } } + public void onRealmCreated(Consumer consumer) { + if (browsingContextIds.isEmpty()) { + this.bidi.addListener(realmCreated, consumer); + } else { + this.bidi.addListener(browsingContextIds, realmCreated, consumer); + } + } + + public void onRealmDestroyed(Consumer consumer) { + if (browsingContextIds.isEmpty()) { + this.bidi.addListener(realmDestroyed, consumer); + } else { + this.bidi.addListener(browsingContextIds, realmDestroyed, consumer); + } + } + private Map getCallFunctionParams( String targetType, String id, diff --git a/java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java b/java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java index 5cfae23aea551..604ae3f821f4f 100644 --- a/java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java +++ b/java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java @@ -20,6 +20,7 @@ import static org.openqa.selenium.testing.Safely.safelyCall; import static org.openqa.selenium.testing.drivers.Browser.CHROME; import static org.openqa.selenium.testing.drivers.Browser.EDGE; +import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; import static org.openqa.selenium.testing.drivers.Browser.IE; import static org.openqa.selenium.testing.drivers.Browser.SAFARI; @@ -33,10 +34,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.bidi.Script; +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.environment.webserver.NettyAppServer; import org.openqa.selenium.testing.JupiterTestBase; import org.openqa.selenium.testing.NotYetImplemented; +import org.openqa.selenium.testing.Pages; public class ScriptEventsTest extends JupiterTestBase { private AppServer server; @@ -52,7 +55,7 @@ public void setUp() { @NotYetImplemented(IE) @NotYetImplemented(EDGE) @NotYetImplemented(CHROME) - void canAddPreloadScriptWithChannelOptions() + void canListenToChannelMessage() throws ExecutionException, InterruptedException, TimeoutException { try (Script script = new Script(driver)) { CompletableFuture future = new CompletableFuture<>(); @@ -78,6 +81,47 @@ void canAddPreloadScriptWithChannelOptions() } } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) + @NotYetImplemented(CHROME) + void canListenToRealmCreatedEvent() + throws ExecutionException, InterruptedException, TimeoutException { + try (Script script = new Script(driver)) { + CompletableFuture future = new CompletableFuture<>(); + script.onRealmCreated(future::complete); + + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); + + context.navigate(new Pages(server).blankPage); + RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS); + assertThat(realmInfo.getRealmId()).isNotNull(); + assertThat(realmInfo.getRealmType()).isEqualTo(RealmType.WINDOW); + } + } + + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) + @NotYetImplemented(CHROME) + @NotYetImplemented(FIREFOX) + void canListenToRealmDestroyedEvent() + throws ExecutionException, InterruptedException, TimeoutException { + try (Script script = new Script(driver)) { + CompletableFuture future = new CompletableFuture<>(); + script.onRealmDestroyed(future::complete); + + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); + + context.close(); + RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS); + assertThat(realmInfo.getRealmId()).isNotNull(); + assertThat(realmInfo.getRealmType()).isEqualTo(RealmType.WINDOW); + } + } + @AfterEach public void quitDriver() { if (driver != null) {