WebDriver - How do I check for an alert?

In selenium2 (Webdriver) How to check for alert? and keep doing something if he is not there !!!

I'm doing it:

driver.findElement(By.id("btn_may_or_maynot_showalert")).click();
WebDriverWait wait = new WebDriverWait(driver, 2);
try{
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    alert.accept();
}
catch (Exception e){
    System.out.println("No alert");
}
driver.findElement(By.id("Cont_doing_something")).click();

      

This works great. But is there a better way?

+3


source to share


1 answer


No, you do as the library expects. However, one of the principles of the library is that you should always know what to expect from your automation code. This means that you should not run into an instance where the "may or may not" button raises a warning; you should already know if pressing the button will trigger an alert or not. If it does anything other than what you expect, an exceptional condition and exception should be thrown.



+2


source







All Articles