Example usage for org.openqa.selenium NotFoundException NotFoundException

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

Introduction

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

Prototype

public NotFoundException(String message, Throwable cause) 

Source Link

Usage

From source file:org.nuxeo.functionaltests.AbstractTest.java

License:Open Source License

/**
 * Finds the first {@link WebElement} using the given method, with a
 * {@code findElementTimeout}. Then waits until the element is enabled,
 * with a {@code waitUntilEnabledTimeout}.
 *
 * @param by the locating mechanism//from  w  w  w  . j av a  2 s  .c  om
 * @param findElementTimeout the find element timeout in milliseconds
 * @param waitUntilEnabledTimeout the wait until enabled timeout in
 *            milliseconds
 * @return the first matching element on the current page, if found
 * @throws NotFoundException if the element is not found or not enabled
 */
public static WebElement findElementAndWaitUntilEnabled(By by, int findElementTimeout,
        int waitUntilEnabledTimeout) throws NotFoundException {

    // Find the element.
    WebElement element = findElementWithTimeout(by, findElementTimeout);

    // Try to wait until the element is enabled.
    Clock clock = new SystemClock();
    long end = clock.laterBy(findElementTimeout);
    WebDriverException lastException = null;
    while (clock.isNowBefore(end)) {
        try {
            waitUntilEnabled(element, waitUntilEnabledTimeout);
            return element;
        } catch (StaleElementReferenceException sere) {
            // Means the element is no longer attached to the DOM
            // => need to find it again.
            element = findElementWithTimeout(by, findElementTimeout);
            lastException = sere;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignore
        }
    }
    throw new NotFoundException(String.format("Couldn't find element '%s' after timeout", by), lastException);
}

From source file:org.nuxeo.functionaltests.AbstractTest.java

License:Open Source License

/**
 * Finds the first {@link WebElement} using the given method, with a
 * {@code findElementTimeout}. Then waits until the element is enabled,
 * with a {@code waitUntilEnabledTimeout}. Then clicks on the element.
 *
 * @param by the locating mechanism/*from w ww. ja  v a2  s .c  o m*/
 * @param findElementTimeout the find element timeout in milliseconds
 * @param waitUntilEnabledTimeout the wait until enabled timeout in
 *            milliseconds
 * @throws NotFoundException if the element is not found or not enabled
 */
public static void findElementWaitUntilEnabledAndClick(By by, int findElementTimeout,
        int waitUntilEnabledTimeout) throws NotFoundException {

    // Find the element.
    WebElement element = findElementAndWaitUntilEnabled(by, findElementTimeout, waitUntilEnabledTimeout);

    // Try to click on the element.
    Clock clock = new SystemClock();
    long end = clock.laterBy(findElementTimeout);
    WebDriverException lastException = null;
    while (clock.isNowBefore(end)) {
        try {
            element.click();
            return;
        } catch (ElementNotVisibleException enve) {
            // Means the element is no visible yet
            // => need to find it again.
            element = findElementAndWaitUntilEnabled(by, findElementTimeout, waitUntilEnabledTimeout);
            lastException = enve;
        } catch (StaleElementReferenceException sere) {
            // Means the element is no longer attached to the DOM
            // => need to find it again.
            element = findElementAndWaitUntilEnabled(by, findElementTimeout, waitUntilEnabledTimeout);
            lastException = sere;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignore
        }
    }
    throw new NotFoundException(String.format("Couldn't find element '%s' after timeout", by), lastException);
}