@Test public void basicPromptConfirmHandlingChangeAndDismissTest() { WebElement promptButton; WebElement promptResult; promptButton = driver.findElement(By.id("promptexample")); promptResult = driver.findElement(By.id("promptreturn")); assertEquals("pret", promptResult.getText()); promptButton.click(); String alertMessage = "I prompt you"; Alert promptAlert = driver.switchTo().alert(); if (Driver.currentDriver != Driver.BrowserName.IE) { // no point doing this in IE as we know it isn't the actual prompt assertEquals(alertMessage, promptAlert.getText()); } promptAlert.sendKeys("Hello"); promptAlert.dismiss(); assertEquals("pret", promptResult.getText()); }
@Test public void basicPromptConfirmHandlingDismissTest() { WebElement promptButton; WebElement promptResult; promptButton = driver.findElement(By.id("promptexample")); promptResult = driver.findElement(By.id("promptreturn")); assertEquals("pret", promptResult.getText()); promptButton.click(); String alertMessage = "I prompt you"; Alert promptAlert = driver.switchTo().alert(); if (Driver.currentDriver == Driver.BrowserName.IE) { // In IE the alert always returns "Script Prompt:" and not the // actual prompt text, so this line is just to alert me if the // functionality changes if (!promptAlert.getText().equals("Script Prompt:")) { throw new RuntimeException("IE now does not do Script Prompt"); } } else { // only check the alert prompt if not in IE assertEquals(alertMessage, promptAlert.getText()); } promptAlert.dismiss(); assertEquals("pret", promptResult.getText()); }
private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } }
@JavascriptEnabled @Ignore({ANDROID, HTMLUNIT, IE, IPHONE, SELENESE}) public void testShouldAllowUsersToDismissAnAlertManually() { if (!isCapableOfHandlingAlerts(driver)) { return; } driver.get(alertPage); driver.findElement(By.id("alert")).click(); Alert alert = switchToAlert(driver); alert.dismiss(); // If we can perform any action, we're good to go assertEquals("Testing Alerts", driver.getTitle()); }
@Ignore public void testAlertShouldNotAllowAdditionalCommandsIfDimissed() { if (!isCapableOfHandlingAlerts(driver)) { return; } driver.get(alertPage); driver.findElement(By.id("alert")).click(); Alert alert = switchToAlert(driver); alert.dismiss(); try { alert.getText(); } catch (NoAlertPresentException expected) { } }
@Test public void basicConfirmHandlingDismissTest() { WebElement confirmButton; WebElement confirmResult; confirmButton = driver.findElement(By.id("confirmexample")); confirmResult = driver.findElement(By.id("confirmreturn")); assertEquals("cret", confirmResult.getText()); confirmButton.click(); String alertMessage = "I am a confirm alert"; Alert confirmAlert = driver.switchTo().alert(); assertEquals(alertMessage, confirmAlert.getText()); confirmAlert.dismiss(); assertEquals("false", confirmResult.getText()); }
public void cancelAlert() { Alert alert = driver.switchTo().alert(); alert.dismiss(); }