I am trying to verify presence of alert windows and then accepting it. While doing that using the below code, when NO alert window was present it is causing an exception to be thrown, which is NOT wanted, thus asked to modify the code.
public void acceptIfAlertPresent()
{
driver.sleep(2000);
try{
if(driver.switchTo().alert() != null){
String alertMessage=driver.switchTo().alert().getText();
try
{
driver.switchTo().alert().accept();
}
catch (StackOverflowError e)
{
driver.switchTo().alert().accept();
}
resultMap.putOutput("MessageOnAlertWindow", alertMessage);
}
}catch(Exception e){
}
}
How to modify my method ? Almost all the time, the exception is being thrown from the outer most catch block