diff --git a/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java b/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java index f47955e674bb..5b20009693fa 100644 --- a/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java +++ b/examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java @@ -23,69 +23,70 @@ import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; + import static org.junit.jupiter.api.Assertions.assertEquals; public class AlertsTest { @Test public void testForAlerts() throws Exception { - - ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.addArguments("disable-search-engine-choice-screen"); - WebDriver driver = new ChromeDriver(chromeOptions); - - driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); - driver.manage().window().maximize(); - //Navigate to Url - driver.get("https://www.selenium.dev/documentation/webdriver/interactions/alerts/"); - - //Simple Alert - //Click the link to activate the alert - JavascriptExecutor js = (JavascriptExecutor) driver; - //execute js for alert - js.executeScript("alert('Sample Alert');"); - WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); - //Wait for the alert to be displayed and store it in a variable - wait.until(ExpectedConditions.alertIsPresent()); - - Alert alert=driver.switchTo().alert(); - //Store the alert text in a variable and verify it - String text = alert.getText(); - assertEquals(text,"Sample Alert"); - //Press the OK button - alert.accept(); - - //Confirm - //execute js for confirm - js.executeScript("confirm('Are you sure?');"); - //Wait for the alert to be displayed - wait = new WebDriverWait(driver, Duration.ofSeconds(30)); - wait.until(ExpectedConditions.alertIsPresent()); - - - alert = driver.switchTo().alert(); - //Store the alert text in a variable and verify it - text = alert.getText(); - assertEquals(text,"Are you sure?"); - //Press the Cancel button - alert.dismiss(); - - //Prompt - //execute js for prompt - js.executeScript("prompt('What is your name?');"); - //Wait for the alert to be displayed and store it in a variable - wait = new WebDriverWait(driver, Duration.ofSeconds(30)); - wait.until(ExpectedConditions.alertIsPresent()); - - alert = driver.switchTo().alert(); - //Store the alert text in a variable and verify it - text = alert.getText(); - assertEquals(text,"What is your name?"); - //Type your message - alert.sendKeys("Selenium"); - //Press the OK button - alert.accept(); - //quit the browser - driver.quit(); + + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("disable-search-engine-choice-screen"); + WebDriver driver = new ChromeDriver(chromeOptions); + + driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); + driver.manage().window().maximize(); + //Navigate to Url + driver.get("https://www.selenium.dev/documentation/webdriver/interactions/alerts/"); + + //Simple Alert + //Click the link to activate the alert + JavascriptExecutor js = (JavascriptExecutor) driver; + //execute js for alert + js.executeScript("alert('Sample Alert');"); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + //Wait for the alert to be displayed and store it in a variable + wait.until(ExpectedConditions.alertIsPresent()); + + Alert alert = driver.switchTo().alert(); + //Store the alert text in a variable and verify it + String text = alert.getText(); + assertEquals(text, "Sample Alert"); + //Press the OK button + alert.accept(); + + //Confirm + //execute js for confirm + js.executeScript("confirm('Are you sure?');"); + //Wait for the alert to be displayed + wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + wait.until(ExpectedConditions.alertIsPresent()); + + + alert = driver.switchTo().alert(); + //Store the alert text in a variable and verify it + text = alert.getText(); + assertEquals(text, "Are you sure?"); + //Press the Cancel button + alert.dismiss(); + + //Prompt + //execute js for prompt + js.executeScript("prompt('What is your name?');"); + //Wait for the alert to be displayed and store it in a variable + wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + wait.until(ExpectedConditions.alertIsPresent()); + + alert = driver.switchTo().alert(); + //Store the alert text in a variable and verify it + text = alert.getText(); + assertEquals(text, "What is your name?"); + //Type your message + alert.sendKeys("Selenium"); + //Press the OK button + alert.accept(); + //quit the browser + driver.quit(); } } \ No newline at end of file