I’m using
WebDriver driver = DriverFactory.getWebDriver()
String AlertText = driver.switchTo().alert().getText()
to handle different alerts that may pop up. However, when there are no alerts, I get an error message saying
[ERROR] - Test Cases/MyCase FAILED because (of) org.openqa.selenium.NoAlertPresentException: no alert open
How can I make the “getText()” optional?
You will probably want to use the waitForAleryPresent, combined with getAlertText.
I’ve used this piece of code for something similar.
elementPresent=WebUI.waitForAlert(5)
if (elementPresent==true) {
alertText = WebUI.getAlertText()
log.logWarning('The title of the alert is:=' + alertText)
if (alertText=='There was an error retreiving results'){
log.logError('ERROR: There was an error retrieving results on the Page')
KeywordUtil.markFailed('ERROR: There was an error retrieving results on the Prospect Page')
WebUI.acceptAlert()
}
}