Skip to content

Commit

Permalink
[java][bidi] Add browsing contexts events related to navigation and u…
Browse files Browse the repository at this point in the history
…ser prompt
  • Loading branch information
pujagani committed Oct 17, 2023
1 parent 4b3238f commit d689900
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
Expand Up @@ -121,11 +121,11 @@ private void onBrowsingContextDestroyed(Consumer<BrowsingContextInfo> consumer)
}
}

private void onNavigationStarted(Consumer<NavigationInfo> consumer) {
public void onNavigationStarted(Consumer<NavigationInfo> consumer) {
addNavigationEventListener("browsingContext.navigationStarted", consumer);
}

private void onFragmentNavigated(Consumer<NavigationInfo> consumer) {
public void onFragmentNavigated(Consumer<NavigationInfo> consumer) {
addNavigationEventListener("browsingContext.fragmentNavigated", consumer);
}

Expand All @@ -149,15 +149,15 @@ private void onNavigationFailed(Consumer<NavigationInfo> consumer) {
addNavigationEventListener("browsingContext.navigationFailed", consumer);
}

private void onUserPromptClosed(Consumer<UserPromptClosed> consumer) {
public void onUserPromptClosed(Consumer<UserPromptClosed> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(userPromptClosed, consumer);
} else {
this.bidi.addListener(browsingContextIds, userPromptClosed, consumer);
}
}

private void onUserPromptOpened(Consumer<UserPromptOpened> consumer) {
public void onUserPromptOpened(Consumer<UserPromptOpened> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(userPromptOpened, consumer);
} else {
Expand Down
Expand Up @@ -24,9 +24,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.Ignore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.BrowsingContextInspector;
import org.openqa.selenium.environment.webserver.AppServer;
Expand Down Expand Up @@ -122,6 +125,88 @@ void canListenToBrowsingContextLoadedEvent()
}
}

@Test
@Ignore
void canListenToNavigationStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onNavigationStarted(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
assertThat(navigationInfo.getUrl()).contains("/bidi/logEntryAdded.html");
}
}

@Test
@Ignore
void canListenToFragmentNavigatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/linked_image.html"), ReadinessState.COMPLETE);

inspector.onFragmentNavigated(future::complete);

context.navigate(
server.whereIs("/linked_image.html#linkToAnchorOnThisPage"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
assertThat(navigationInfo.getUrl()).contains("linkToAnchorOnThisPage");
}
}

@Test
@Ignore
void canListenToUserPromptOpenedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<UserPromptOpened> future = new CompletableFuture<>();

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
inspector.onUserPromptOpened(future::complete);

driver.get(server.whereIs("/alerts.html"));

driver.findElement(By.id("alert")).click();

UserPromptOpened userPromptOpened = future.get(5, TimeUnit.SECONDS);
assertThat(userPromptOpened.getBrowsingContextId()).isEqualTo(context.getId());
assertThat(userPromptOpened.getType()).isEqualTo(UserPromptType.ALERT);
}
}

@Test
@Ignore
void canListenToUserPromptClosedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<UserPromptClosed> future = new CompletableFuture<>();

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
inspector.onUserPromptClosed(future::complete);

driver.get(server.whereIs("/alerts.html"));

driver.findElement(By.id("prompt")).click();

context.handleUserPrompt(true, "selenium");

UserPromptClosed userPromptClosed = future.get(5, TimeUnit.SECONDS);
assertThat(userPromptClosed.getBrowsingContextId()).isEqualTo(context.getId());
assertThat(userPromptClosed.getUserText().isPresent()).isTrue();
assertThat(userPromptClosed.getUserText().get()).isEqualTo("selenium");
assertThat(userPromptClosed.getAccepted()).isTrue();
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
Expand Down

0 comments on commit d689900

Please sign in to comment.