Example usage for org.openqa.selenium By toString

List of usage examples for org.openqa.selenium By toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

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

License:Apache License

/**
 * This method wait for text to be present.
 *
 * @param driver/*from w w w . j  a  v a2 s  . c o m*/
 * @param locator
 * @param text
 * @param timeout - in seconds
 * @return
 */
public String WaitForTextPresence(final WebDriver driver, final By locator, final String textToWait,
        final Integer timeout, final Integer pollingTime) {
    this.log.debug("WaitForTextPresence::Enter");
    this.log.debug("Locator: " + locator.toString());
    String textPresent = "";
    ExecutorService executor = null;
    RunnableWaitForText r = new RunnableWaitForText(driver, timeout, pollingTime, locator, textToWait);
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
        if (r.isTextEquals()) { // If the text is equals then send the text that we wait for.
            textPresent = textToWait;
            this.log.debug("Wait for text successful! We got this [" + textPresent + "].");
        } else {
            textPresent = r.getTextPresent();
            this.log.debug("No text present. We only found this [" + textPresent + "].");
        }
    } 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());
            textPresent = r.getTextPresent();
        } 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("WaitForTextPresence::Exit");
    return textPresent;
}

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

License:Apache License

/**
 * This method wait for text to be present but should be different from empty 
 * or null.//w ww.jav  a  2 s  . c  o m
 *
 * @param driver
 * @param locator
 * @param timeout - in seconds
 * @param pollingTime
 * @return
 */
public String WaitForTextDifferentEmpty(final WebDriver driver, final By locator, final Integer timeout,
        final Integer pollingTime) {
    this.log.debug("WaitForTextDifferentEmpty::Enter");
    this.log.debug("Locator: " + locator.toString());
    String textPresent = "";
    ExecutorService executor = null;
    RunnableWaitForTextDifferentEmpty r = new RunnableWaitForTextDifferentEmpty(driver, timeout, pollingTime,
            locator);
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
        textPresent = r.getTextPresent();
    } 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());
            textPresent = r.getTextPresent();
        } 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("WaitForTextPresence::Exit");
    return textPresent;
}

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

License:Apache License

/**
 * The function will search for the element and then click on it using
 * the Click method of java script.//w  w  w  .j a v  a 2  s. c  o  m
 *
 * @param driver
 * @param locator
 */
public void ClickJS(WebDriver driver, By locator) {
    this.log.debug("ClickJS::Enter");
    this.log.debug("Locator: " + locator.toString());

    WebElement element = WaitForElementPresenceAndVisible(driver, locator);
    if (element != null) {
        try {
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("arguments[0].click();", element);
        } catch (WebDriverException wde) {
            if (wde.getMessage().contains("arguments[0].click is not a function")) {
                element.click();
            }
        }
    } else {
        this.log.error("Element is null " + locator.toString());
    }

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

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

License:Apache License

/**
 * The function will search for the element and then click on it using
 * the Click method of webdriver.//from w  w w .  ja v  a  2 s.c  o  m
 *
 * @param driver
 * @param locator
 */
public void Click(WebDriver driver, By locator) {
    this.log.debug("Click::Enter");
    this.log.debug("Locator: " + locator.toString());

    try {
        WebElement element = WaitForElementPresenceAndVisible(driver, locator);
        if (element != null) {
            element.click();
            this.log.debug("Click::Exit");
        } else {
            this.log.error("Element is null " + locator.toString());
        }
    } catch (StaleElementReferenceException e) {
        this.log.warn("Stale Element Reference Exception");
        Click(driver, locator);
    }
}

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

License:Apache License

/**
 * The function will search for the element present (doesn't matter
 * if element is visible or not) and then click on it.
 *
 * @param driver/*from  w ww.  j  a v a 2  s  .c  om*/
 * @param locator
 */
public void ClickElementInvisible(WebDriver driver, By locator) {
    this.log.debug("ClickElementInvisible::Enter");
    this.log.debug("Locator: " + locator.toString());

    WebElement element = FindElementInvisible(driver, locator);
    if (element != null) {
        try {
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("arguments[0].click();", element);
        } catch (WebDriverException wde) {
            if (wde.getMessage().contains("arguments[0].click is not a function")) {
                element.click();
            }
        }
    } else {
        this.log.error("Element is null " + locator.toString());
    }

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

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

License:Apache License

/**
 * The function will search for the element present (doesn't matter
 * if element is visible or not) and then click on it.
 *
 * @param driver/*from  w w w.  ja  v a2 s  .  c  o  m*/
 * @param locator
 */
public void MoveToElementAndClick(WebDriver driver, By locator) {
    this.log.debug("MoveToElementAndClick::Enter");

    try {
        WebElement element = WaitForElementPresenceAndVisible(driver, locator);
        if (element != null) {
            Actions builder = new Actions(driver);
            builder.moveToElement(element).click(element);
            builder.perform();
        } else {
            this.log.error("Element is null " + locator.toString());
        }
    } catch (StaleElementReferenceException sere) {
        //Repeat it again
        MoveToElementAndClick(driver, locator);
    }

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

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// w w w  . jav a 2 s  .  c om
 * 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/*from   w  w w. j a v a 2s . c  om*/
 * @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  w ww  .ja v a 2s  .  co  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//from  w w w . j ava  2  s.co  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;
}