Search in sources :

Example 1 with WebSessionEndedException

use of com.seleniumtests.customexception.WebSessionEndedException in project seleniumRobot by bhecquet.

the class DriverExceptionListener method onException.

@Override
public void onException(final Throwable ex, final WebDriver arg1) {
    if (ex.getMessage() == null || ex.getMessage().contains("Element must be user-editable in order to clear it") || ex.getMessage().contains("Element is not clickable at point") || ex instanceof UnsupportedCommandException || ex.getMessage().contains(" read-only") || ex.getMessage().contains("not implemented") || ex instanceof org.openqa.selenium.UnsupportedCommandException || // exception raised when element is non clickable
    ex instanceof MoveTargetOutOfBoundsException || // exception raised when element is non clickable
    ex instanceof InvalidElementStateException) {
    // do nothing
    // Edge driver does return a WebDriverException when doing getPageSource
    } else if (ex.getMessage().contains("No response on ECMAScript evaluation command")) {
        // customexception
        for (int i = 0; i < ex.getStackTrace().length; i++) {
            String method = ex.getStackTrace()[i].getMethodName();
            if (method.contains("getTitle") || method.contains("getWindowHandle") || method.contains("click") || method.contains("getPageSource")) {
                return;
            }
        }
        logger.error(ex);
    } else if (ex.getMessage().contains("Error communicating with the remote browser. It may have died.")) {
        // Session has lost connection, remove it then ignore quit() method.
        if (WebUIDriver.getWebUIDriver(false).getConfig().getMode() == DriverMode.GRID) {
            WebUIDriver.setWebDriver(null);
            throw new WebSessionEndedException(ex);
        }
    } else if (ex instanceof NoSuchWindowException) {
        try {
            WebDriver driver = WebUIDriver.getWebDriver(false);
            List<String> handles = new ArrayList<>(driver.getWindowHandles());
            if (!handles.isEmpty()) {
                try {
                    driver.switchTo().window(handles.get(handles.size() - 1));
                    logger.info("Current window has been closed, switching to previous window to avoid problems in future commands");
                } catch (IndexOutOfBoundsException | NoSuchWindowException e) {
                    driver.switchTo().window(handles.get(0));
                    logger.info("Current window has been closed, switching to first window to avoid problems in future commands");
                }
            }
        } catch (Exception e) {
        // ignore, do not raise exception during handling it
        }
    } else {
        String message = ex.getMessage().split("\\n")[0];
        logger.warn("Got exception:" + message);
        if (ex instanceof org.openqa.selenium.remote.UnreachableBrowserException || ex instanceof NoSuchSessionException || message.matches("Session .*? was terminated due to.*") || message.matches("Session .*? not available .*") || message.matches("cannot forward the request .*") || message.matches("Session is closed") || message.contains("Unable to get browser") || message.contains("not reachable") || message.contains("Tried to run command without establishing a connection") || message.matches("Session ID is null.*") || message.contains("java.net.ConnectException: Failed to connect") || message.contains("java.net.ConnectException: Connection refused")) {
            // can't quit anymore, save time.
            WebUIDriver.setWebDriver(null);
            // terminated.
            throw new WebSessionEndedException(ex);
        // issue #281: chrome < 73: WebDriverException is raised instead of ElementClickInterceptedException
        } else if (message.contains("Other element would receive the click")) {
            throw new ElementClickInterceptedException(message);
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) UnsupportedCommandException(org.openqa.selenium.UnsupportedCommandException) NoSuchSessionException(org.openqa.selenium.NoSuchSessionException) ElementClickInterceptedException(org.openqa.selenium.ElementClickInterceptedException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) MoveTargetOutOfBoundsException(org.openqa.selenium.interactions.MoveTargetOutOfBoundsException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) WebSessionEndedException(com.seleniumtests.customexception.WebSessionEndedException) ElementClickInterceptedException(org.openqa.selenium.ElementClickInterceptedException) NoSuchSessionException(org.openqa.selenium.NoSuchSessionException) MoveTargetOutOfBoundsException(org.openqa.selenium.interactions.MoveTargetOutOfBoundsException) UnsupportedCommandException(org.openqa.selenium.UnsupportedCommandException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) WebSessionEndedException(com.seleniumtests.customexception.WebSessionEndedException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) ArrayList(java.util.ArrayList) List(java.util.List) UnsupportedCommandException(org.openqa.selenium.UnsupportedCommandException)

Aggregations

WebSessionEndedException (com.seleniumtests.customexception.WebSessionEndedException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ElementClickInterceptedException (org.openqa.selenium.ElementClickInterceptedException)1 InvalidElementStateException (org.openqa.selenium.InvalidElementStateException)1 NoSuchSessionException (org.openqa.selenium.NoSuchSessionException)1 NoSuchWindowException (org.openqa.selenium.NoSuchWindowException)1 UnsupportedCommandException (org.openqa.selenium.UnsupportedCommandException)1 WebDriver (org.openqa.selenium.WebDriver)1 MoveTargetOutOfBoundsException (org.openqa.selenium.interactions.MoveTargetOutOfBoundsException)1