diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index bc8b33a6e068b..edfb71ef283c7 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -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, diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java index ba50fa9c4854f..33927f4876c44 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java @@ -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; @@ -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; @@ -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( + "click me")); + } + + 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("", + "

")); + } + @AfterEach public void quitDriver() { if (driver != null) {