I need to check the existence of Alert in WebDriver.
Sometimes it pops up an alert but sometimes it will not pop up. I need to check if the alert exists first, then
I would suggest to use ExpectedConditions and alertIsPresent(). ExpectedConditions is a wrapper class that implements useful conditions defined in ExpectedCondition interface.
WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
if(wait.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("alert was not present");
else
System.out.println("alert was present");
I found catching exception of driver.switchTo().alert();
is so slow in Firefox
(FF V20 & selenium-java-2.32.0).`
So I choose another way:
private static boolean isDialogPresent(WebDriver driver) {
try {
driver.getTitle();
return false;
} catch (UnhandledAlertException e) {
// Modal dialog showed
return true;
}
}
And it's a better way when most of your test cases is NO dialog present (throwing exception is expensive).
public boolean isAlertPresent()
{
try
{
driver.switchTo().alert();
return true;
} // try
catch (NoAlertPresentException Ex)
{
return false;
} // catch
} // isAlertPresent()
check the link here https://groups.google.com/forum/?fromgroups#!topic/webdriver/1GaSXFK76zY