Skip to content

Commit

Permalink
[bidi][java] Add browsing context handle user prompt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Sep 20, 2023
1 parent 79f73c4 commit 465bb1e
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,21 @@ private NavigationResult reload(boolean ignoreCache, ReadinessState readinessSta
navigationInfoMapper));
}

// Yet to be implemented by browser vendors
private void handleUserPrompt() {
public void handleUserPrompt() {
this.bidi.send(new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id)));
}

// Yet to be implemented by browser vendors
private void handleUserPrompt(String userText) {
public void handleUserPrompt(boolean accept) {
this.bidi.send(
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "accept", accept)));
}

public void handleUserPrompt(String userText) {
this.bidi.send(
new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "userText", userText)));
}

// Yet to be implemented by browser vendors
private void handleUserPrompt(boolean accept, String userText) {
public void handleUserPrompt(boolean accept, String userText) {
this.bidi.send(
new Command<>(
HANDLE_USER_PROMPT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
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;
Expand All @@ -29,10 +30,12 @@
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.BiDiException;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.environment.webserver.Page;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NotYetImplemented;

Expand Down Expand Up @@ -231,6 +234,132 @@ void canReloadWithReadinessState() {
assertThat(reloadInfo.getUrl()).contains("/bidi/logEntryAdded.html");
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canHandleUserPrompt() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(alertPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

browsingContext.handleUserPrompt();

assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canAcceptUserPrompt() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(alertPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

browsingContext.handleUserPrompt(true);

assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canDismissUserPrompt() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(alertPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

browsingContext.handleUserPrompt(false);

assertThat(driver.getTitle()).isEqualTo("Testing Alerts");
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canPassUserTextToUserPrompt() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(promptPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

String userText = "Selenium automates browsers";

browsingContext.handleUserPrompt(userText);

assertThat(driver.getPageSource()).contains(userText);
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canAcceptUserPromptWithUserText() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(promptPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

String userText = "Selenium automates browsers";

browsingContext.handleUserPrompt(true, userText);

assertThat(driver.getPageSource()).contains(userText);
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
void canDismissUserPromptWithUserText() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(promptPage());

driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());

String userText = "Selenium automates browsers";

browsingContext.handleUserPrompt(false, userText);

assertThat(driver.getPageSource()).doesNotContain(userText);
}

private String alertPage() {
return appServer.create(
new Page()
.withTitle("Testing Alerts")
.withBody(
"<a href='#' id='alert' onclick='alert(\"works\");'>click me</a>"));
}

private String promptPage() {
return appServer.create(
new Page()
.withTitle("Testing Alerts")
.withScripts(
"function myFunction() {",
" let message = prompt('Please enter a message');",
" if (message != null) {",
" document.getElementById('result').innerHTML =",
" 'Message: ' + message ;",
" }",
"}")
.withBody("<button id='alert' onclick='myFunction()'>Try it</button>",
"<p id=\"result\"></p>"));
}

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

0 comments on commit 465bb1e

Please sign in to comment.