Skip to content

Commit ea51452

Browse files
committed
[bidi][java] Add realm related events
1 parent bb4b80d commit ea51452

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

java/src/org/openqa/selenium/bidi/Script.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ public class Script implements Closeable {
7474
}
7575
});
7676

77+
private final Event<RealmInfo> realmCreated =
78+
new Event<>(
79+
"script.realmCreated",
80+
params -> {
81+
try (StringReader reader = new StringReader(JSON.toJson(params));
82+
JsonInput input = JSON.newInput(reader)) {
83+
return input.read(RealmInfo.class);
84+
}
85+
});
86+
87+
private final Event<RealmInfo> realmDestroyed =
88+
new Event<>(
89+
"script.realmDestroyed",
90+
params -> {
91+
try (StringReader reader = new StringReader(JSON.toJson(params));
92+
JsonInput input = JSON.newInput(reader)) {
93+
return input.read(RealmInfo.class);
94+
}
95+
});
96+
7797
public Script(WebDriver driver) {
7898
this(new HashSet<>(), driver);
7999
}
@@ -314,6 +334,22 @@ public void onMessage(Consumer<Message> consumer) {
314334
}
315335
}
316336

337+
public void onRealmCreated(Consumer<RealmInfo> consumer) {
338+
if (browsingContextIds.isEmpty()) {
339+
this.bidi.addListener(realmCreated, consumer);
340+
} else {
341+
this.bidi.addListener(browsingContextIds, realmCreated, consumer);
342+
}
343+
}
344+
345+
public void onRealmDestroyed(Consumer<RealmInfo> consumer) {
346+
if (browsingContextIds.isEmpty()) {
347+
this.bidi.addListener(realmDestroyed, consumer);
348+
} else {
349+
this.bidi.addListener(browsingContextIds, realmDestroyed, consumer);
350+
}
351+
}
352+
317353
private Map<String, Object> getCallFunctionParams(
318354
String targetType,
319355
String id,

java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.openqa.selenium.testing.Safely.safelyCall;
2121
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
2222
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
23+
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2324
import static org.openqa.selenium.testing.drivers.Browser.IE;
2425
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2526

@@ -33,10 +34,12 @@
3334
import org.junit.jupiter.api.BeforeEach;
3435
import org.junit.jupiter.api.Test;
3536
import org.openqa.selenium.bidi.Script;
37+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
3638
import org.openqa.selenium.environment.webserver.AppServer;
3739
import org.openqa.selenium.environment.webserver.NettyAppServer;
3840
import org.openqa.selenium.testing.JupiterTestBase;
3941
import org.openqa.selenium.testing.NotYetImplemented;
42+
import org.openqa.selenium.testing.Pages;
4043

4144
public class ScriptEventsTest extends JupiterTestBase {
4245
private AppServer server;
@@ -52,7 +55,7 @@ public void setUp() {
5255
@NotYetImplemented(IE)
5356
@NotYetImplemented(EDGE)
5457
@NotYetImplemented(CHROME)
55-
void canAddPreloadScriptWithChannelOptions()
58+
void canListenToChannelMessage()
5659
throws ExecutionException, InterruptedException, TimeoutException {
5760
try (Script script = new Script(driver)) {
5861
CompletableFuture<Message> future = new CompletableFuture<>();
@@ -78,6 +81,47 @@ void canAddPreloadScriptWithChannelOptions()
7881
}
7982
}
8083

84+
@Test
85+
@NotYetImplemented(SAFARI)
86+
@NotYetImplemented(IE)
87+
@NotYetImplemented(EDGE)
88+
@NotYetImplemented(CHROME)
89+
void canListenToRealmCreatedEvent()
90+
throws ExecutionException, InterruptedException, TimeoutException {
91+
try (Script script = new Script(driver)) {
92+
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
93+
script.onRealmCreated(future::complete);
94+
95+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
96+
97+
context.navigate(new Pages(server).blankPage);
98+
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
99+
assertThat(realmInfo.getRealmId()).isNotNull();
100+
assertThat(realmInfo.getRealmType()).isEqualTo(RealmType.WINDOW);
101+
}
102+
}
103+
104+
@Test
105+
@NotYetImplemented(SAFARI)
106+
@NotYetImplemented(IE)
107+
@NotYetImplemented(EDGE)
108+
@NotYetImplemented(CHROME)
109+
@NotYetImplemented(FIREFOX)
110+
void canListenToRealmDestroyedEvent()
111+
throws ExecutionException, InterruptedException, TimeoutException {
112+
try (Script script = new Script(driver)) {
113+
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
114+
script.onRealmDestroyed(future::complete);
115+
116+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
117+
118+
context.close();
119+
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
120+
assertThat(realmInfo.getRealmId()).isNotNull();
121+
assertThat(realmInfo.getRealmType()).isEqualTo(RealmType.WINDOW);
122+
}
123+
}
124+
81125
@AfterEach
82126
public void quitDriver() {
83127
if (driver != null) {

0 commit comments

Comments
 (0)