I am writing a Selenium test on Firefox that deals with an alert. The alert appears for a fraction of a second when the test is run, but when done manually the alert persists. C
There are 3 states for UnexpectedAlertBehaviour
:
ACCEPT
- Accepts the alertDISMISS
- Closes/Cancels the alertIGNORE
- Neither accepts nor closes the alertDesiredCapabilities dc = new DesiredCapabilities(); dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE); driver = new FirefoxDriver(dc);
Then you can handle the alert by performing the operation that triggers the alert and catching it as an expected exception:
try { click(myButton); } catch (UnhandledAlertException f) { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); System.out.println("Alert data: " + alertText); alert.accept(); } catch (NoAlertPresentException e) { e.printStackTrace(); } }