@Test public void test() throws InterruptedException { // Alert popup Handling. driver.findElement(By.xpath("input[@value='Show Me Alert']")).click(); // To locate alert Alert A1 = driver.switchTo().alert(); // To read the text from alert popup. String Alert1 = A1.getText(); System.out.println(Alert1); Thread.sleep(2000); // To accept/Click OK on the alert popup A1.accept(); // Confirmation Pop up handling. driver.findElement(By.xpath("//button[@onlick='myFunction()']")).click(); Alert A2 = driver.switchTo().alert(); String Alert2 = A2.getText(); System.out.println(Alert2); Thread.sleep(2000); // To Click on cancel button of confirmation box. A2.dismiss(); // Prompt pop up handling. driver.findElement(By.xpath("//button[contains(.,'Show Me Prompt')]")).click(); Alert A3 = driver.switchTo().alert(); String Alert3 = A3.getText(); System.out.println(Alert3); // To type text Im text box of prompt pop up. A3.sendKeys("This is John"); Thread.sleep(2000); A3.accept(); }
@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()); }
public static void main(String[] args) throws InterruptedException { WebDriver dr = new ChromeDriver(); File file = new File("src/form.html"); String filePath = "file:///" + file.getAbsolutePath(); System.out.printf("now accesss %s \n", filePath); dr.get(filePath); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=checkbox]")).click(); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=radio]")).click(); Thread.sleep(1000); List<WebElement> options = dr.findElement(By.tagName("select")).findElements(By.tagName("option")); options.get(options.size() - 1).click(); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=submit]")).click(); Alert alert = dr.switchTo().alert(); System.out.println(alert.getText()); alert.accept(); Thread.sleep(1000); System.out.println("browser will be close"); dr.quit(); }
public TrashSubPage emptyTrash() { findElementWaitUntilEnabledAndClick(By.id(EMPTY_TRASH_BUTTON_ID)); Alert alert = driver.switchTo().alert(); assertEquals("Permanently delete all documents in trash?", alert.getText()); alert.accept(); return asPage(TrashSubPage.class); }
protected File savePageSourceToFile(String fileName, WebDriver webdriver, boolean retryIfAlert) { File pageSource = new File(reportsFolder, fileName + ".html"); try { writeToFile(webdriver.getPageSource(), pageSource); } catch (UnhandledAlertException e) { if (retryIfAlert) { try { Alert alert = webdriver.switchTo().alert(); log.severe(e + ": " + alert.getText()); alert.accept(); savePageSourceToFile(fileName, webdriver, false); } catch (Exception unableToCloseAlert) { log.severe("Failed to close alert: " + unableToCloseAlert); } } else { printOnce("savePageSourceToFile", e); } } catch (UnreachableBrowserException e) { writeToFile(e.toString(), pageSource); return pageSource; } catch (Exception e) { writeToFile(e.toString(), pageSource); printOnce("savePageSourceToFile", e); } return pageSource; }
@Test public void basicPromptConfirmHandlingAcceptTest() { 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.accept(); assertEquals("change me", promptResult.getText()); }
// Get Text public String getTextFromAlert() { Utils.pause(1000); try { Alert alert = driver.switchTo().alert(); return alert.getText(); } catch (NoAlertPresentException e) { return ""; } }
public static void VerifyPopUpForDelete() throws Exception { GenericClass.clickElement(FstCheckBox); GenericClass.clickElement(DeleteSeasonBtn); Alert al = GenericClass.driver.switchTo().alert(); al.getText(); al.dismiss(); System.out.print("Alert Message"); Reporter.log("Pop up showing when deleting Season", true); }
@Override public String getAlert() { switchTo(); WebDriverWait webDriverWait = new WebDriverWait(this, 1); Alert alert = webDriverWait.until(ExpectedConditions.alertIsPresent()); return alert.getText(); }
/** @param model Verify the alert text */ public void verifyalertText(MethodParameters model) { model.getElement().get(0).click(); wait1(1000); Alert alert = WebDriverClass.getInstance().switchTo().alert(); wait1(1000); if (!(alertText == null)) { alertText = null; } alertText = alert.getText(); Assert.assertEquals(alertText.toString(), model.getData()); alert.accept(); }
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; } }
protected String closeAlert(boolean confirm) { Alert alert = null; try { alert = webDriverCache.getCurrent().switchTo().alert(); String text = alert.getText().replace("\n", ""); if (!confirm) { alert.dismiss(); } else { alert.accept(); } return text; } catch (WebDriverException wde) { throw new Selenium2LibraryNonFatalException("There were no alerts"); } }
public static String alertConfirm() { String alertText = ""; try { Alert alert = driver.switchTo().alert(); alertText = alert.getText(); alert.dismiss(); // or accept as below // alert.accept(); } catch (NoAlertPresentException nape) { // nothing to do, because we only want to close it when pop up System.out.println("Alert not found!"); } return alertText; }
public static String getConfirmation(WebDriver webDriver) { webDriver.switchTo(); WebDriverWait webDriverWait = new WebDriverWait(webDriver, 1); try { Alert alert = webDriverWait.until(ExpectedConditions.alertIsPresent()); String confirmation = alert.getText(); alert.accept(); return confirmation; } catch (Exception e) { throw new WebDriverException(); } }
@Test public void textInputAlertTest() { driver.findElement(MobileBy.AccessibilityId("alert_views_button")).click(); driver.findElement(MobileBy.AccessibilityId("text_entry_alert_button")).click(); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); String titleAndMessage = alert.getText(); assertThat( titleAndMessage, is("A Short Title Is Best A message should be a short, complete sentence.")); // input text String text_alert_message = "testing alert text input field"; alert.sendKeys(text_alert_message); String alertTextInputField_value = driver.findElement(MobileBy.xpath("//UIAAlert//UIATextField")).getText(); assertThat(alertTextInputField_value, is(text_alert_message)); }
@Test public void handlingSimpleAlertTest() { final String expected_alert_text = "A Short Title Is Best A message should be a short, complete sentence."; driver.findElement(MobileBy.AccessibilityId("alert_views_button")).click(); // wait for alert view to load by waiting for "simple" alert button wait.until( ExpectedConditions.visibilityOf( driver.findElement(MobileBy.AccessibilityId("simple_alert_button")))) // and click on it .click(); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); String titleAndMessage = alert.getText(); assertThat(titleAndMessage, is(expected_alert_text)); alert.accept(); }
@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 static void CancelDeletePopUp() throws Exception { String Season = GenericClass.getText(FirstSeason); GenericClass.clickElement(FstCheckBox); GenericClass.clickElement(DeleteSeasonBtn); Alert A1 = GenericClass.driver.switchTo().alert(); Reporter.log("Alert detected", true); String Alert1 = A1.getText(); System.out.println("Pop_UpText :" + Alert1); A1.dismiss(); int trcount = GenericClass.tr_count(CheckBox); int tr = trcount + 2; for (int i = 2; i <= tr; i++) { String getSeason = GenericClass.driver .findElement(By.xpath("//table[@id='mainSeasonTable']//tr[" + i + "]//td[3]")) .getText(); if (getSeason.equals(Season)) { System.out.println(Season + " Matched"); break; } } }
public static void itemEntry(WebDriver d) { WebElement slot = null; try { slot = d.findElement(By.xpath("//div[@class='slots']")); } catch (NoSuchElementException e) { return; } slot.click(); try { d.findElement(By.xpath("//button[@id='getslots']")).click(); } catch (NoSuchElementException e) { return; } try { Alert alert = d.switchTo().alert(); if (!alert.getText().equals("Success!")) { alert.accept(); itemEntry(d); } alert.accept(); } catch (NoAlertPresentException e) { return; } }
public void clickLogout(WebDriver driver) { driver.findElement(logout).click(); Alert alert = driver.switchTo().alert(); System.out.println("Alert Present on page: " + alert.getText()); alert.accept(); }
protected void deleteSelectedDocuments() { findElementWaitUntilEnabledAndClick(By.id(PERMANENT_DELETE_BUTTON_ID)); Alert alert = driver.switchTo().alert(); assertEquals("Permanently delete selected document(s)?", alert.getText()); alert.accept(); }