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

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

Introduction

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

Prototype

public FluentWait(T input) 

Source Link

Usage

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method pretends to check if the element is present, if it doesn't
 * then don't wait, if element is present, wait for its invisibility.
 * true - element  invisible//from ww w . j  a v  a 2  s.  c o  m
 * false - element visible
 * @param driver
 * @param locator
 * @param timeout
 */
public boolean WaitForElementInvisibility(final WebDriver driver, final By locator, final Integer timeout) {
    this.log.debug("WaitForElementInvisibility::Enter");
    this.log.debug("Locator: " + locator.toString());
    boolean isElemVisible = false;
    ExecutorService executor = null;

    try {

        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

        try {
            class RunnableObject implements Runnable {

                private boolean isVisible;

                public boolean getVisibility() {
                    return this.isVisible;
                }

                public void run() {
                    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                            .withTimeout(timeout, TimeUnit.SECONDS).pollingEvery(50, TimeUnit.MILLISECONDS);

                    // Wait for element invisible
                    this.isVisible = wait.until(new Function<WebDriver, Boolean>() {

                        @Override
                        public Boolean apply(WebDriver d) {
                            try {
                                List<WebElement> listElements = d.findElements(locator);
                                if (listElements.size() > 0) {
                                    WebElement elem = listElements.get(0);
                                    return (!elem.isDisplayed()) ? true : false;
                                }
                                // The element does not exit, i.e., is not visible and even present
                                return true;
                            } catch (StaleElementReferenceException sere) {
                                return false;
                            }
                        }
                    });
                }
            }

            RunnableObject r = new RunnableObject();
            executor = Executors.newSingleThreadExecutor();
            executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
            isElemVisible = r.getVisibility();
        } catch (InterruptedException ie) {
            this.log.warn("Interrupted Exception");
            this.log.warn(ie.toString());
        } catch (ExecutionException ee) {
            if (ee.getCause().getClass().getCanonicalName()
                    .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
                this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
                this.log.warn(ee.toString());
            } else {
                this.log.warn("Execution Exception");
                this.log.warn(ee.toString());
            }
        } catch (java.util.concurrent.TimeoutException cte) {
            this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
            this.log.warn(cte.toString());
        } catch (Exception e) {
            this.log.error("Exception");
            this.log.catching(e);
        }

        if (executor != null) {
            executor.shutdown();
        }

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    } catch (Exception ge) {
        this.log.error("Exception");
        this.log.catching(ge);
    }
    this.log.debug("WaitForElementInvisibility::Exit");

    return isElemVisible;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method pretends to check if the element is present, if it doesn't
 * we wait for presence for a specific timeout (input), after this, we will
 * wait for element visible. And, if the element is present then we have to
 * check if is visible if not wait for visibility.
 *
 * @param driver// w  w w .j ava 2s . co m
 * @param locator
 * @param timeout
 */
public WebElement WaitForElementPresenceAndVisible(final WebDriver driver, final By locator,
        final Integer timeout) {
    this.log.debug("WaitForElementPresenceAndVisible::Enter");
    this.log.debug("Locator: " + locator.toString());
    WebElement element = null;
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            private WebElement theElement;

            public RunnableObject(WebElement theElement) {
                this.theElement = theElement;
            }

            public WebElement getValue() {
                return this.theElement;
            }

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                this.theElement = wait.until(new Function<WebDriver, WebElement>() {

                    @Override
                    public WebElement apply(WebDriver d) {
                        try {
                            List<WebElement> listElem = d.findElements(locator);
                            if (listElem.size() > 0) {
                                WebElement elem = listElem.get(0);
                                if (elem != null
                                        && ((elem.isEnabled() == true) && (elem.isDisplayed() == true))) {
                                    return elem;
                                }
                                return null;
                            }
                            return null;
                        } catch (StaleElementReferenceException sere) {
                            return null;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject(element);
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
        element = r.getValue();
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForElementPresenceAndVisible::Exit");
    return element;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method pretends to check if the element is present, if it doesn't
 * we wait for presence for a specific timeout (input).
 *
 * @param driver/*from  ww w. ja v  a  2s.  c  o  m*/
 * @param locator
 */
public WebElement WaitForElementPresence(final WebDriver driver, final By locator, final Integer timeout,
        final Integer pollingtime) {
    this.log.debug("WaitForElementPresence::Enter");
    this.log.debug("Locator: " + locator.toString());
    WebElement element = null;
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            private WebElement theElement = null;

            public RunnableObject() {
            }

            public WebElement getValue() {
                return this.theElement;
            }

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(pollingtime, TimeUnit.MILLISECONDS);

                // Wait for element visible
                this.theElement = wait.until(new Function<WebDriver, WebElement>() {

                    @Override
                    public WebElement apply(WebDriver d) {
                        try {
                            List<WebElement> listElem = d.findElements(locator);
                            if (listElem.size() > 0) {
                                WebElement elem = listElem.get(0);
                                if (elem.isEnabled()) {
                                    return elem;
                                }
                                return null;
                            }
                            return null;
                        } catch (StaleElementReferenceException sere) {
                            return null;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
        element = r.getValue();
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForElementPresence::Exit");
    return element;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method pretends to check if the element is present, if it doesn't
 * then don't wait, if element is present, wait for its invisibility.
 *
 * @param driver/*  w w w  . j  a  v a 2 s .c  o m*/
 * @param locator
 */
public WebElement WaitForElementVisibility(WebDriver driver, By locator) {
    this.log.debug("WaitForElementVisibility::Enter");
    this.log.debug("Locator: " + locator.toString());

    WebElement element = null;
    List<WebElement> elements = null;
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(50, TimeUnit.MILLISECONDS).ignoring(StaleElementReferenceException.class);

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {
        elements = driver.findElements(locator);
        if (elements.size() > 0) {
            // wait for element to appear
            element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
            this.log.debug("Get element visible.");
        } else {
            this.log.warn("No elements found!");
        }
    } catch (Exception e) {
        this.log.warn("Something went wrong searching for vi: " + locator.toString());
        this.log.catching(e);
    }

    // ------------ ALWAYS REQUIRE TO SET THE DEFAULT VALUE --------------------
    // when set a new implicitlyWait timeout, we have to set the default
    // in order to not destroy other invocations of findElement ('WebDriver').
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    this.log.debug("WaitForElementVisibility::Exit");
    return element;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This function shall wait for the new window display.
 * The code check if there is more than one window in the list. In the
 * beginning we only have the main window.
 *
 * @param driver//from  w  ww . ja v  a  2 s. c  o m
 */
public void WaitForNewWindow(WebDriver driver) {
    this.log.debug("WaitForNewWindow::Enter");

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(500,
            TimeUnit.MILLISECONDS);

    wait.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver d) {
            return d.getWindowHandles().size() != 1;
        }
    });

    this.log.debug("WaitForNewWindow::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This function shall wait for the alert window to be closed or doesn't
 * exist./*from  w ww  .j a  va 2 s. c o  m*/
 *
 * @param driver
 */
public void WaitForAlertNotPresent(WebDriver driver) {
    this.log.debug("WaitForAlertNotPresent::Enter");

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(100,
            TimeUnit.MILLISECONDS);

    wait.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver d) {
            Boolean alertExist = false;
            try {
                d.switchTo().alert();
                alertExist = true;
            } catch (NoAlertPresentException e) {
                // Ignore the exception
            }
            return alertExist != true;
        }
    });

    this.log.debug("WaitForAlertNotPresent::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * The method will wait for the frame to be available to usage. To ensure that
 * we check if an element exist inside (example a new element that refresh the
 * frame)./*w  w w  .j  ava2  s  .com*/
 *
 * @param driver
 * @param locator
 */
public void WaitForFrameReady(WebDriver driver, final By locator) {
    this.log.debug("WaitForFrameReady::Enter");
    this.log.debug("Locator: " + locator.toString());

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(100,
            TimeUnit.MILLISECONDS);

    wait.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver d) {
            Boolean elementExist = false;
            List<WebElement> listElements = d.findElements(locator);

            if (listElements.size() > 0) {
                elementExist = true;
            }
            return elementExist;
        }
    });

    this.log.debug("WaitForFrameReady::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method pretends to assert the element is not present and return a
 * boolean = true if it isn't. This method also allow user to set a specify timeout.
 *
 * @param driver/*from  w w w .j av a2s.  c  o  m*/
 * @param locator
 * @param timeout
 */
public Boolean WaitForElementNotPresent(final WebDriver driver, final By locator, final Integer timeout) {
    this.log.debug("WaitForElementNotPresent::Enter");
    Boolean notPresent = false;
    ExecutorService executor = null;
    this.log.debug("Locator: " + locator.toString());
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            private Boolean NotPresent;

            public RunnableObject(Boolean NotPresent) {
                this.NotPresent = NotPresent;
            }

            public Boolean getValue() {
                return this.NotPresent;
            }

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                this.NotPresent = wait.until(new Function<WebDriver, Boolean>() {

                    @Override
                    public Boolean apply(WebDriver d) {
                        try {
                            List<WebElement> listElem = d.findElements(locator);
                            if (listElem.size() == 0) {
                                return true;
                            }
                            return false;
                        } catch (StaleElementReferenceException sere) {
                            return true;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject(notPresent);
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
        notPresent = r.getValue();
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForElementNotPresent::Exit");
    return notPresent;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * The method pretends to wait for an element reach the expected attribute
 * value, specifying a timeout.//from w ww.j av a2  s .c om
 *
 * @param driver
 * @param locator
 * @param attributeName
 * @param attributeValue - attribute value to wait.
 */
public void WaitForAttributeValue(final WebDriver driver, final By locator, final String attributeName,
        final String attributeValue, final Integer timeout) {
    this.log.debug("WaitForAttributeValue::Enter");
    this.log.debug("Locator: " + locator.toString());
    this.log.debug("Attribute: " + attributeName);
    this.log.debug("AttributeValue: " + attributeValue);
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                wait.until(new Function<WebDriver, Boolean>() {

                    @Override
                    public Boolean apply(WebDriver d) {
                        try {
                            List<WebElement> listElements = d.findElements(locator);
                            if (listElements.size() > 0) {
                                WebElement element = listElements.get(0);
                                String attrValue = element.getAttribute(attributeName).toLowerCase();
                                String attrValueFor = attributeValue.toLowerCase();
                                return attrValue.contains(attrValueFor);
                            }
                            return false;
                        } catch (StaleElementReferenceException sere) {
                            return true;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForAttributeValue::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * The method pretends to wait for an element reach the expected attribute
 * value, specifying a timeout./*from  w  w  w  .j ava 2 s .  com*/
 *
 * @param driver
 * @param locator
 * @param attributeName
 * @param attributeValue - attribute value to wait.
 */
public void WaitForAttributeValueEqualsTo(final WebDriver driver, final By locator, final String attributeName,
        final String attributeValue, final Integer timeout) {
    this.log.debug("WaitForAttributeValue::Enter");
    this.log.debug("Locator: " + locator.toString());
    this.log.debug("Attribute: " + attributeName);
    this.log.debug("AttributeValue: " + attributeValue);
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                wait.until(new Function<WebDriver, Boolean>() {

                    @Override
                    public Boolean apply(WebDriver d) {
                        try {
                            List<WebElement> listElements = d.findElements(locator);
                            if (listElements.size() > 0) {
                                WebElement element = listElements.get(0);
                                String attrValue = element.getAttribute(attributeName).toLowerCase();
                                String attrValueFor = attributeValue.toLowerCase();
                                return attrValue.equals(attrValueFor);
                            }
                            return false;
                        } catch (StaleElementReferenceException sere) {
                            return true;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForAttributeValue::Exit");
}