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 intends to get the value of an input field.
 *
 * @param driver/*from   ww  w .j a v  a 2  s . co m*/
 * @param locator
 * @return
 */
public String GetInputValue(WebDriver driver, By locator) {
    this.log.debug("GetInputValue::Enter");
    this.log.debug("Locator: " + locator.toString());

    String attrValue = "";
    WebElement element = FindElement(driver, locator);
    if (element != null) {
        attrValue = element.getAttribute("value");
    }

    this.log.debug("GetInputValue::Exit");
    return attrValue;
}

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

License:Apache License

/**
 * This method intends to drag and drop an element.
 *
 * @param driver//ww  w.j  a va 2 s. c o  m
 * @param from
 * @param to
 * @return
 */
public void DragAndDrop(WebDriver driver, By from, By to) {
    this.log.debug("DragAndDrop::Enter");
    this.log.debug("From: " + from.toString());
    this.log.debug("To: " + to.toString());

    WebElement drag = FindElement(driver, from);
    WebElement drop = FindElement(driver, to);
    if (drag != null && drop != null) {
        new Actions(driver).dragAndDrop(drag, drop).build().perform();
    }

    this.log.debug("DragAndDrop::exit");
}

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

License:Apache License

/**
 * This method intends to check if two elements overlap or are contained inside each other, returns true if
 * elements don't overlap.//from  w  w  w .j  a  v a2s .  c  o m
 *
 * @param driver
 * @param element1
 * @param element2
 * @return
 */
public boolean ElementsNotOverlap(WebDriver driver, By element1, By element2) {
    this.log.debug("ElementsNotOverlap::Enter");
    this.log.debug("Locator1: " + element1.toString());
    this.log.debug("Locator2: " + element2.toString());

    WebElement elem1 = FindElement(driver, element1);
    WebElement elem2 = FindElement(driver, element2);

    // get borders of first element
    Point firstLocation = elem1.getLocation();
    Dimension firstDimension = elem1.getSize();
    int firstLeft = firstLocation.getX();
    int firstTop = firstLocation.getY();
    int firstRight = firstLeft + firstDimension.getWidth();
    int firstBottom = firstTop + firstDimension.getHeight();
    // get borders of second element
    Point secondLocation = elem2.getLocation();
    Dimension secondDimension = elem2.getSize();
    int secondLeft = secondLocation.getX();
    int secondTop = secondLocation.getY();
    int secondRight = secondLeft + secondDimension.getWidth();
    int secondBottom = secondTop + secondDimension.getHeight();
    this.log.debug(firstTop + " " + firstBottom + " " + firstLeft + " " + firstRight);
    this.log.debug(secondTop + " " + secondBottom + " " + secondLeft + " " + secondRight);
    // if firstElement is either to the left, the right, above or below the second return true
    boolean notIntersected = firstBottom < secondTop || firstTop > secondBottom || firstLeft > secondRight
            || firstRight < secondLeft;

    this.log.debug("ElementsNotOverlap::Exit");
    return notIntersected;
}

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)./*from w ww  .j  a va2s  .  co  m*/
 *
 * @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//w  w w. ja  v  a 2s. co  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./* w w w . j  a  v a2 s.  com*/
 *
 * @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./* w  w  w  .  jav  a  2 s  .c  o m*/
 *
 * @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");
}

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

License:Apache License

/**
 * This method shall select an element on a drop-down list by Value.
 * /*  w w w .  j av a2 s .c  om*/
 * @param driver
 * @param locator
 * @param value
 */
public void SelectByValue(final WebDriver driver, final By locator, final String value) {
    this.log.debug("SelectByValue::Enter");
    this.log.debug("Locator: " + locator.toString());

    try {
        WebElement elementSelector = WaitForElementPresence(driver, locator);
        if (elementSelector != null) {
            Select list = new Select(elementSelector);
            list.selectByValue(value);
        } else {
            this.log.warn("The element does not exist [null]. Could perform the select action!");
        }
    } catch (StaleElementReferenceException e) {
        this.log.warn("Stale Element Reference Exception");
        SelectByValue(driver, locator, value);
    }
    this.log.debug("SelectByValue::Exit");
}

From source file:com.salesforce.selenium.support.event.Step.java

License:Open Source License

public static String getLocatorFromBy(By by) {
    return (by != null) ? getLocatorFromBy(by.toString()) : null;
}

From source file:com.seleniumtests.uipage.htmlelements.HtmlElement.java

License:Apache License

/**
 * Find elements inside this element//from   w w  w  . j  a va  2s.  com
 * @param by
 * @return    List of selenium WebElement 
 */
@Override
@ReplayOnError
public List<WebElement> findElements(By by) {

    // find the root element
    findElement(false, false);
    List<WebElement> elements = element.findElements(by);

    // throw exception so that behavior is the same as with 'findElements()' call which retries search
    if (elements.isEmpty()) {
        throw new NoSuchElementException("No elements found for " + by.toString());
    } else {
        return elements;
    }
}