I am using EvenfiringWebdriver and code executes fine. I am intentionally giving a wrong xpath so as to make sure my framework captures NoSuchElementException or TimeOutException and later i will handle this in the block the way i require. But the problem is in the beforeFindBy method the exception occurs and the onException method does not catch this, instead the exception is thrown and displayed on my console.
If i just set the right xpath , everything works fine, so my framework is ok , for instance if i close the browser while the test are executing then the onException block is called and WebDriver exception code is triggered, only in case of expected condition and wait until the exception is not captured.
public class EventHandler extends AbstractWebDriverEventListener {
private final static Logger logger = Logger.getLogger(Wrapper.class.getName());
protected EventFiringWebDriver driver = TheDriver.getInstance().getDriver();
protected static WebDriverWait waitn;
protected JavascriptExecutor js;
// public static WebDriverWait waitn;
@Override
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
ensurePageIsLoaded(); // before anything be sure you are ready!
logger.info("Verifying if element " + by.toString() + " is interactable...");
//WebDriverWait waitn = new WebDriverWait(driver, 100 * Integer.parseInt(Wrapper.dataOf("FluentDelay")[0]));
waitn = new WebDriverWait(driver, 100 * 2);
waitn.until(ExpectedConditions.presenceOfElementLocated(by));
}
@Override
public void onException(Throwable throwable, WebDriver driver) {
//String theException = throwable.getClass().toString();
logger.warn("Entered the exception block");
if (throwable instanceof NoSuchElementException) {
logger.error(
"The element was either not present or was uninteractable due to an iFrame or a wrong locator");
// logger.error(throwable.getMessage());
} else if (throwable instanceof TimeoutException) {
logger.error("Expected wait time reached. Webdriver wait has timed out.");
} else if (throwable instanceof WebDriverException) {
logger.error("Unable to interact with browser sessions, closing the browser");
} else if (throwable instanceof UnhandledAlertException) {
logger.error("An Alert box poped up and was not handled by the script");
logger.error(throwable.getMessage());
} else if (throwable instanceof ElementClickInterceptedException) {
logger.warn(
"Some popup appeared while performing a click , Scripts will reatempt to click after a short delay");
Wrapper.hangon(5);
} else if (throwable instanceof Exception) {
logger.error("Unknown error");
logger.error(throwable.getMessage());
} else {
logger.error("Unknown error. The exception is not handled" + throwable.getMessage());
}
}
The below line of code is never executed when exception occurs
logger.warn("Entered the exception block");
Please help me understand the issue.