Example usage for org.openqa.selenium TimeoutException TimeoutException

List of usage examples for org.openqa.selenium TimeoutException TimeoutException

Introduction

In this page you can find the example usage for org.openqa.selenium TimeoutException TimeoutException.

Prototype

public TimeoutException(Throwable cause) 

Source Link

Usage

From source file:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Waits for all asynchronous calls to complete by checking javascript
 * framework used by the product to have zero active connections. Increases
 * sleep interval time as duration increases, finally using configured sleep
 * interval after 30 seconds.//from   www  .j  a va  2 s  .  c  o m
 *
 * For jQuery products, resets counter jQuery.activeError to zero at the end
 * of each wait.
 */
public void waitForAjax() {
    if (!waitForAjaxEnabled) {
        return;
    }
    int timeout = waitForAjaxTimeout;
    int idle = waitForAjaxIdle;
    int sleep = 1;
    int sleepAfter = waitForAjaxSleepAfter;
    int duration = 0;
    StringBuilder jsQuery = new StringBuilder();
    jsQuery.append("return (");
    jsQuery.append("(typeof jQuery === 'undefined' || jQuery == null) ? 0 : ");
    jsQuery.append("((jQuery.active == 0) ? 0 : (jQuery.active - ");
    jsQuery.append("((jQuery.activeError == undefined) ? 0 : jQuery.activeError)");
    jsQuery.append(")))");
    while (true) {
        // wrap in a try block in case javascript executes fail
        long ajaxActive = 0;
        try {
            ajaxActive = (Long) executeJavaScript(jsQuery.toString());
            log.debug("ajaxActive count: {}", ajaxActive);

            if (ajaxActive < 0) {
                log.warn("connection errors more than active connections");
            }

            // when active calls are zero wait in a cooling off period
            if (ajaxActive <= 0) {
                log.debug("ajax active calls is zero");
                if (idle > 0) {
                    log.debug("sleeping {} seconds before confirming idle state...");
                    try {
                        Thread.sleep(idle * 1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                // TODO change idles and other waits to milliseconds

                // if ajax remains inactive, wait is complete
                ajaxActive = (Long) executeJavaScript(jsQuery.toString());
                log.debug("ajaxActive count: {}", ajaxActive);
                if (ajaxActive <= 0) {
                    log.info("confirmed no ajax calls currently pending");
                    break;
                }
            }

            // increase sleep interval as duration increases
            if (duration < 15) {
                sleep = 1;
            } else if (duration < 30) {
                sleep = 2;
            } else if (duration < 45) {
                sleep = 3;
            } else {
                sleep = waitForAjaxSleep;
            }
        } finally {
            // only poll if there are active connections
            if (ajaxActive > 0) {
                try {
                    if (duration >= timeout) {
                        String message = "timeout waiting for active ajax count to be zero," + " currently "
                                + ajaxActive + " active after " + duration + " seconds";
                        log.warn(message);
                        throw new TimeoutException(message);
                    }

                    // updating polling
                    log.debug("sleeping for {} seconds...", sleep);
                    Thread.sleep(sleep * 1000);

                    duration += sleep;
                    log.debug("...polled for " + duration + " seconds");

                } catch (Exception e) {
                    // this is not typically a fatal error to timeout
                    // TODO add configurable to let exception through
                    break;
                }
            }
        }
    }
    if (sleepAfter > 0) {
        log.debug("sleeping for {} seconds after async idle...", sleepAfter);
        try {
            Thread.sleep(sleepAfter * 1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.testeditor.fixture.web.WebFixture.java

License:Open Source License

/**
 * This Method finds WebElements with a given {@link By}. If the given
 * {@link By} is an XPath expression and matches multiple elements, this
 * method will return the first visible element (as determined by selenium's
 * {@link isDisplayed} method) that matches.
 * //from  w w w  . ja v  a2  s.com
 * @param context
 *            the search context or subtree to query. Argument
 *            <code>context</code> may be <code>null</code> to search the
 *            whole document.
 * @param by
 *            Mechanism used to locate elements within a document
 * @return the webElement
 * @throws TimeoutException
 *             if element is not found
 */
protected WebElement waitForElement(final WebElement context, final By by) throws TimeoutException {
    List<WebElement> elements = null;
    WebElement result = null;
    int counter = 0;

    while (elements == null) {
        if (counter >= waitCounter) {
            break;
        }

        try {
            if (context == null) {
                elements = webDriver.findElements(by);
            } else {
                elements = context.findElements(by);
            }
        } catch (Exception e) {
            elements = null;
        }

        if (elements != null) {
            result = getFirstDisplayed(elements);
            if (result != null) {
                break;
            }
        }

        elements = null;
        waitTime(waitInMillis);
        counter++;
    }

    if (result == null || !result.isDisplayed()) {
        throw new TimeoutException("Timeout: no element was found");
    }

    return result;
}

From source file:org.uiautomation.ios.context.BaseWebInspector.java

License:Apache License

private RemoteWebElement retrieveDocumentAndCheckReady(long deadline) {
    RemoteWebElement element = null;// w w  w  . j ava2s.c om
    String readyState = "";
    while (!readyState.equals("complete")) {
        if (deadline > 0 && System.currentTimeMillis() > deadline) {
            throw new TimeoutException("Timeout waiting to get the document.");
        }
        try {
            log.fine("trying to get the document");
            element = retrieveDocument();
            log.fine("got it");
            readyState = element.getRemoteObject().call(".readyState");
            log.fine("ready ? " + readyState);
        } catch (Exception e) {
            log.warning("The given document is corrupted, nodeId="
                    + ((element != null) ? element.getNodeId() : "null") + ": " + e);
            throw new WebKitSeemsCorruptedException();

        }
    }
    return element;
}

From source file:org.uiautomation.ios.server.DOMContext.java

License:Apache License

public RemoteWebElement getDocument() {
    int cpt = 0;/*w  w  w  . j  a va  2s.  c om*/
    while (!isReady) {
        cpt++;
        if (cpt > 20) {
            isReady = true;
            throw new TimeoutException("doc not ready.");
        }
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return document;
}

From source file:org.uiautomation.ios.server.DOMContext.java

License:Apache License

public void waitForPageToLoad(long timeout) {
    long start = System.currentTimeMillis();
    long deadLine = start + timeout;
    while (!pageLoaded) {
        if (System.currentTimeMillis() > deadLine) {
            // TODO freynaud check for alert while page loads.
            throw new TimeoutException("failed to load the page after " + timeout + " ms.");
        }/*w ww . ja v a2  s . c  om*/
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // ignore
        }
    }

    pageLoaded = false;
    waitForDocumentReady(deadLine);
    return;
}

From source file:org.uiautomation.ios.server.DOMContext.java

License:Apache License

private void waitForDocumentReady(long deadLine) {
    while (!isReady()) {
        if (System.currentTimeMillis() > deadLine) {
            throw new TimeoutException("failed to load the page");
        }/*from  ww  w . j av  a 2  s .c om*/
    }
}

From source file:org.uiautomation.ios.server.DOMContext.java

License:Apache License

public void waitForLoadEvent() {
    try {/*  ww  w  .j av  a 2 s  . c o m*/
        eventsLock.lock();
        pageLoadEvent.await(30, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        throw new TimeoutException("timeout waiting for page load event.");
    } finally {
        eventsLock.unlock();
    }
}

From source file:org.uiautomation.ios.server.instruments.CommunicationChannel.java

License:Apache License

public UIAScriptResponse waitForResponse() throws InterruptedException {
    UIAScriptResponse res = responseQueue.poll(IOSCapabilities.COMMAND_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    if (res == null)
        throw new TimeoutException("waiting for UIAScriptResponse");
    return res;//from  w  ww. j  a  v  a  2 s  . c  o m
}

From source file:org.xwiki.test.ui.po.LiveTableElement.java

License:Open Source License

/**
 * Wait till the livetable has finished displaying all its rows (so that we can then assert the livetable content
 * without running the risk that the rows have not been updated yet).
 */// w w  w  .  j a v a2  s. c  o  m
public void waitUntilReady() {
    long t1 = System.currentTimeMillis();
    while ((System.currentTimeMillis() - t1 < getUtil().getTimeout() * 1000L)) {
        if (isReady()) {
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // Do nothing just break out
            break;
        }
    }
    throw new TimeoutException("Livetable isn't ready after the timeout has expired.");
}