WebDriver dismiss an Alert box

Take a look at issue 27 in the Google Code Issues List. Use the JavascriptExecutor to override the browser's alert:

((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");

It's a similar solution to handle confirmation dialogs.

((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");

This is a pretty popular request so hopefully it'll get implemented soon. The downside to these approaches is that it's not easy to validate the text in the message. You may want to step down to Selenium to do that.


driver.findElement(By.id("updateButton")).click();

//pop up with id "updateButton" opens

Alert alert = driver.switchTo().alert();
//update is executed
alert.accept();

Tags:

Webdriver