Pages

Saturday, 19 May 2012

How to check the presence of Alert (isAlertPresent) using WebDriver?

Below method will return true if the alert got displayed:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

 }
Note - Thank you Ganesh S for asking this query.

1 comment:

  1. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    Thanks a lot for taking a time to share a wonderful article.
    Best Selenium Training Institute in Chennai |
    Selenium Training in Velachery

    ReplyDelete