Example usage for org.openqa.selenium.support.ui FluentWait until

List of usage examples for org.openqa.selenium.support.ui FluentWait until

Introduction

In this page you can find the example usage for org.openqa.selenium.support.ui FluentWait until.

Prototype

@Override
public <V> V until(Function<? super T, V> isTrue) 

Source Link

Document

Repeatedly applies this instance's input value to the given function until one of the following occurs:
  1. the function returns neither null nor false
  2. the function throws an unignored exception
  3. the timeout expires
  4. the current thread is interrupted

Usage

From source file:org.alfresco.po.PageElement.java

License:Open Source License

/**
 * Helper method to find a {@link WebElement} with a time limit in milliseconds. During the wait period it will check for the element every 100 millisecond.
 * //from   ww w .  ja v  a2 s .co  m
 * @param by {@link By} criteria to search by
 * @param limit time limit
 * @param interval polling frequency
 * @return {@link WebElement} HTML element
 */
public WebElement findAndWaitInNestedElement(final By by, final long limit, final long interval) {
    FluentWait<By> fluentWait = new FluentWait<By>(by);
    fluentWait.pollingEvery(interval, TimeUnit.MILLISECONDS);
    fluentWait.withTimeout(limit, TimeUnit.MILLISECONDS);
    fluentWait.until(new Predicate<By>() {
        public boolean apply(By by) {
            try {
                return getWrappedElement().findElement(by).isDisplayed();
            } catch (NoSuchElementException ex) {
                return false;
            }
        }
    });
    return getWrappedElement().findElement(by);
}

From source file:org.alfresco.po.PageElement.java

License:Open Source License

/**
 * Helper method to find a {@link WebElement} with a time limit in
 * milliseconds. During the wait period it will check for the element every
 * 100 millisecond. If the element is not displayed, refresh the page.
 *
 * @param by {@link By} criteria to search by
 * @return {@link WebElement}/*from  w w w.j  av a  2  s .  c o  m*/
 */
public WebElement findAndWaitWithRefresh(final By by, final long limit) {
    if (null == by) {
        throw new IllegalArgumentException("A search By criteria is required");
    }
    FluentWait<By> fluentWait = new FluentWait<By>(by);
    fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
    fluentWait.withTimeout(limit, TimeUnit.MILLISECONDS);
    fluentWait.ignoring(NoSuchElementException.class);
    fluentWait.until(new Predicate<By>() {
        public boolean apply(By by) {
            try {
                return driver.findElement(by).isDisplayed();
            } catch (NoSuchElementException ex) {
                driver.navigate().refresh();
                return false;
            }
        }
    });
    return driver.findElement(by);
}

From source file:org.eclipse.che.selenium.pageobject.dashboard.organization.AddMember.java

License:Open Source License

private void waitForElementDisappearance(String xpath) {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(seleniumWebDriver)
            .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
            .pollingEvery(200, TimeUnit.MILLISECONDS).ignoring(StaleElementReferenceException.class);

    wait.until((Function<WebDriver, Boolean>) driver -> {
        List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
        return elements.isEmpty() || elements.get(0).isDisplayed();
    });//from w ww  .  j ava 2s . com
}

From source file:org.eclipse.che.selenium.pageobject.dashboard.organization.AddMember.java

License:Open Source License

/**
 * Wait for popup is attached to DOM and animation ends.
 *
 * @param xpath xpath to match the 'che-popup' element
 *///ww  w  . j ava2 s.c  om
private void waitForPopupAppearence(String xpath) {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(seleniumWebDriver)
            .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
            .pollingEvery(200, TimeUnit.MILLISECONDS).ignoring(StaleElementReferenceException.class);

    Map<String, Integer> lastSize = new HashMap<>();
    lastSize.put("height", 0);
    lastSize.put("width", 0);

    wait.until((Function<WebDriver, Boolean>) driver -> {
        List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
        if (elements.isEmpty()) {
            return false;
        }

        Dimension size = elements.get(0).getSize();
        if (lastSize.get("height") < size.getHeight() || lastSize.get("width") < size.getHeight()) {
            lastSize.put("height", size.getHeight());
            lastSize.put("width", size.getWidth());

            return false;
        }

        return true;
    });
}

From source file:org.eclipse.che.selenium.pageobject.dashboard.organization.OrganizationListPage.java

License:Open Source License

private void waitForElementDisappearance(String xpath) {
    FluentWait<SeleniumWebDriver> wait = new FluentWait<>(seleniumWebDriver)
            .withTimeout(REDRAW_UI_ELEMENTS_TIMEOUT_SEC, TimeUnit.SECONDS)
            .pollingEvery(200, TimeUnit.MILLISECONDS).ignoring(StaleElementReferenceException.class);

    wait.until((Function<WebDriver, Boolean>) driver -> {
        List<WebElement> elements = seleniumWebDriver.findElements(By.xpath(xpath));
        return elements.isEmpty() || !elements.get(0).isDisplayed();
    });//from   w w  w  .  j a v  a 2  s  .c  o m
}

From source file:org.eclipse.che.selenium.pageobject.TestWebElementRenderChecker.java

License:Open Source License

private void waitElementIsStatic(FluentWait<WebDriver> webDriverWait, WebElement webElement) {
    AtomicInteger sizeHashCode = new AtomicInteger();

    webDriverWait.until((ExpectedCondition<Boolean>) driver -> {
        Dimension newDimension = waitAndGetWebElement(webElement).getSize();

        if (dimensionsAreEquivalent(sizeHashCode, newDimension)) {
            return true;
        } else {//from  w  ww .j a v  a  2s . c om
            sizeHashCode.set(getSizeHashCode(newDimension));
            return false;
        }
    });
}

From source file:org.keycloak.quickstart.uma.page.PhotozPage.java

License:Apache License

public void waitForPageToLoad() {

    if (webDriver instanceof HtmlUnitDriver) {
        return; // not needed
    }/* w  w w. j  ava2 s.c om*/

    // Ensure the URL is "stable", i.e. is not changing anymore; if it'd changing, some redirects are probably still in progress
    for (int maxRedirects = 2; maxRedirects > 0; maxRedirects--) {
        String currentUrl = webDriver.getCurrentUrl();
        FluentWait<WebDriver> wait = new FluentWait<>(webDriver)
                .withTimeout(Duration.of(250, ChronoUnit.MILLIS));
        try {
            wait.until(not(urlToBe(currentUrl)));
        } catch (TimeoutException e) {
            break; // URL has not changed recently - ok, the URL is stable and page is current
        }
        if (maxRedirects == 1) {
            log.warn("URL seems unstable! (Some redirect are probably still in progress)");
        }
    }

    WebDriverWait wait = new WebDriverWait(webDriver, PAGELOAD_TIMEOUT_MILLIS / 1000);

    try {
        // Checks if the document is ready and asks AngularJS, if present, whether there are any REST API requests
        // in progress
        wait.until(javaScriptThrowsNoExceptions("if (document.readyState !== 'complete' "
                + "|| (typeof angular !== 'undefined' && angular.element(document.body).injector().get('$http').pendingRequests.length !== 0)) {"
                + "throw \"Not ready\";" + "}"));
    } catch (TimeoutException e) {
        // Sometimes, for no obvious reason, the browser/JS doesn't set document.readyState to 'complete' correctly
        // but that's no reason to let the test fail; after the timeout the page is surely fully loaded
        log.warn("waitForPageToLoad time exceeded!");
    }
}

From source file:org.keycloak.testsuite.util.WaitUtils.java

License:Apache License

/**
 * Waits for page to finish any pending redirects, REST API requests etc.
 * Because Keycloak's Admin Console is a single-page application, we need to
 * take extra steps to ensure the page is fully loaded
 *///from  w w  w. j av  a2  s. co  m
public static void waitForPageToLoad() {
    WebDriver driver = getCurrentDriver();

    if (driver instanceof HtmlUnitDriver) {
        return; // not needed
    }

    String currentUrl = null;

    // Ensure the URL is "stable", i.e. is not changing anymore; if it'd changing, some redirects are probably still in progress
    for (int maxRedirects = 4; maxRedirects > 0; maxRedirects--) {
        currentUrl = driver.getCurrentUrl();
        FluentWait<WebDriver> wait = new FluentWait<>(driver).withTimeout(Duration.ofMillis(250));
        try {
            wait.until(not(urlToBe(currentUrl)));
        } catch (TimeoutException e) {
            break; // URL has not changed recently - ok, the URL is stable and page is current
        }
        if (maxRedirects == 1) {
            log.warn("URL seems unstable! (Some redirect are probably still in progress)");
        }
    }

    WebDriverWait wait = new WebDriverWait(getCurrentDriver(), PAGELOAD_TIMEOUT_MILLIS / 1000);
    ExpectedCondition waitCondition = null;

    // Different wait strategies for Admin and Account Consoles
    if (currentUrl.matches("^[^\\/]+:\\/\\/[^\\/]+\\/auth\\/admin\\/.*$")) { // Admin Console
        // Checks if the document is ready and asks AngularJS, if present, whether there are any REST API requests in progress
        waitCondition = javaScriptThrowsNoExceptions("if (document.readyState !== 'complete' "
                + "|| (typeof angular !== 'undefined' && angular.element(document.body).injector().get('$http').pendingRequests.length !== 0)) {"
                + "throw \"Not ready\";" + "}");
    } else if (currentUrl.matches("^[^\\/]+:\\/\\/[^\\/]+\\/auth\\/realms\\/[^\\/]+\\/account\\/.*$") // check for Account Console URL
            && driver.getPageSource().contains("patternfly-ng") // check for new Account Console (don't use this strategy with the old one)
    ) {
        waitCondition = javaScriptThrowsNoExceptions(
                "if (!window.getAngularTestability(document.querySelector('app-root')).isStable()) {"
                        + "throw 'Not ready';" + "}");
    }

    if (waitCondition != null) {
        try {
            wait.until(waitCondition);
        } catch (TimeoutException e) {
            log.warn("waitForPageToLoad time exceeded!");
        }
    }
}

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

License:Apache License

public static List<WebElement> findElementsWithTimeout(final By by) throws NoSuchElementException {
    FluentWait<WebDriver> wait = getFluentWait();
    wait.ignoring(NoSuchElementException.class);
    return wait.until(new Function<WebDriver, List<WebElement>>() {
        @Override/*from   w  w w.  j  a va  2  s  . com*/
        public List<WebElement> apply(WebDriver driver) {
            List<WebElement> elements = driver.findElements(by);
            return elements.isEmpty() ? null : elements;
        }
    });
}

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

License:Apache License

/**
 * Finds the first {@link WebElement} using the given method, with a timeout.
 *
 * @param by the locating mechanism//  www.ja  va2s. c  om
 * @param timeout the timeout in milliseconds
 * @param parentElement find from the element
 * @return the first matching element on the current page, if found
 * @throws NoSuchElementException when not found
 */
public static WebElement findElementWithTimeout(final By by, int timeout, final WebElement parentElement)
        throws NoSuchElementException {
    FluentWait<WebDriver> wait = getFluentWait();
    wait.withTimeout(timeout, TimeUnit.MILLISECONDS).ignoring(StaleElementReferenceException.class);
    try {
        return wait.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver driver) {
                try {
                    if (parentElement == null) {
                        return driver.findElement(by);
                    } else {
                        return parentElement.findElement(by);
                    }
                } catch (NoSuchElementException e) {
                    return null;
                }
            }
        });
    } catch (TimeoutException e) {
        throw new NoSuchElementException(String.format("Couldn't find element '%s' after timeout", by));
    }
}