Skip to content

Commit

Permalink
[bidi][java] Add realm related events
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Nov 3, 2023
1 parent bb4b80d commit ea51452
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
36 changes: 36 additions & 0 deletions java/src/org/openqa/selenium/bidi/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public class Script implements Closeable {
}
});

private final Event<RealmInfo> 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<RealmInfo> 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);
}
Expand Down Expand Up @@ -314,6 +334,22 @@ public void onMessage(Consumer<Message> consumer) {
}
}

public void onRealmCreated(Consumer<RealmInfo> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(realmCreated, consumer);
} else {
this.bidi.addListener(browsingContextIds, realmCreated, consumer);
}
}

public void onRealmDestroyed(Consumer<RealmInfo> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(realmDestroyed, consumer);
} else {
this.bidi.addListener(browsingContextIds, realmDestroyed, consumer);
}
}

private Map<String, Object> getCallFunctionParams(
String targetType,
String id,
Expand Down
46 changes: 45 additions & 1 deletion java/test/org/openqa/selenium/bidi/script/ScriptEventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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<Message> future = new CompletableFuture<>();
Expand All @@ -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<RealmInfo> 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<RealmInfo> 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) {
Expand Down

0 comments on commit ea51452

Please sign in to comment.