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:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Finds and gets input value attribute of specified element.
 *
 * @param by//from   w ww  .j av a  2s.co  m
 *            web driver identifier
 *
 * @return String value else null if no element is found to be selected
 */
public String getInputValue(By by) {
    WebElement webElement = null;
    List<WebElement> elements = findElements(by);

    // for single elements, check if it's a select menu
    if (elements.size() == 1) {
        if (elements.get(0).getTagName().equals("select")) {
            webElement = new Select(elements.get(0)).getFirstSelectedOption();
        } else {
            webElement = elements.get(0);
        }
    } else {
        // search for the selected value to return
        if (elements.get(0).getAttribute("type").equals("radio")
                || elements.get(0).getAttribute("type").equals("checkbox")) {
            for (WebElement element : elements) {
                if (element.isSelected()) {
                    webElement = element;
                    break;
                }
            }
        } else {
            log.warn("multiple elements found, but unable to process more than the first");
            webElement = elements.get(0);
        }
    }

    // return null if nothing is selected
    if (webElement == null) {
        log.warn("no input element was found to be selected based on locator {}", by.toString());
        return null;
    }

    // normalize empty string values to nulls as they would be defined in
    // most expected output objects
    String value = webElement.getAttribute("value");
    if (value.equals("")) {
        return null;
    }
    return value;
}

From source file:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Finds and sends input to field only if the value specified is not null.
 * If field is already populated with something (e.g. a text field), the
 * value specified will be appended to the existing value in the field.
 *
 * @param by//from   w w  w  . j a v a2s . co m
 *            web driver identifier
 * @param value
 *
 * @return null if value is null, else the WebElement operated on
 */
public WebElement sendInput(By by, Object value) {
    if (value == null) {
        return null;
    }
    log.info("sending input value [{}] to element [{}]...", value.toString(), by.toString());
    return setInput(by, value, false);
}

From source file:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Finds and edits field's values only if the value specified is not null.
 * Clears input element and then sends keys.
 *
 * @param by//from  ww w. j  ava 2  s  .c  om
 *            web driver identifier
 * @param value
 *
 * @return null if value is null, else the WebElement operated on
 */
public WebElement editInput(By by, Object value) {
    if (value == null) {
        return null;
    }
    log.info("editing input value [{}] to element [{}]...", value.toString(), by.toString());
    return setInput(by, value, true);
}

From source file:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Finds the input element specified and sets the value on it depending on
 * the value and whether it should be cleared first.
 *
 * @param by//from  w ww  . j  a v a 2s  . c  om
 *            web driver identifier
 * @param value
 * @param clear
 *            whether to clear the field before setting the input
 * @return null if value is null, else the WebElement operated on
 */
private WebElement setInput(By by, Object value, boolean clear) {
    OpenWebElement webElement = null;
    if (value != null) {
        // get all elements in case of radios or other multi input types
        List<WebElement> elements = findElements(by);
        if (elements.size() == 0) {
            throw new NoSuchElementException("input element not found with by: " + by.toString());
        }
        webElement = (OpenWebElement) elements.get(0);

        // identifying string used for log messages
        String elementInfo = OpenWebElement.extractElementInfo(webElement);

        // process elements by tag name
        String tagName = webElement.getTagName().toLowerCase();
        if (tagName.toLowerCase().equals("input")) {

            // process elements of input tag by type
            String type = webElement.getAttribute("type").toLowerCase();
            if (type.equals("text") || type.equals("password") || type.equals("number") || type.equals("url")) {
                if (clear) {
                    webElement.clear();
                }
                webElement.sendKeys(value.toString());
            } else if (type.equals("radio")) {
                // search through radio elements for the matching value
                for (WebElement element : elements) {
                    if (element.getAttribute("value").equals(value.toString())) {
                        webElement = (OpenWebElement) element;
                        break;
                    }
                }
                log.info("clicking radio element value [{}]...", value.toString());
                webElement.clickNoWait();
            } else if (type.equals("checkbox")) {
                Boolean check = (Boolean) value;
                if (webElement.isSelected()) {
                    log.info("checkbox [{}] is currently selected...", elementInfo);
                    if (!check) {
                        log.info("unchecking checkbox [{}]...", elementInfo);
                        webElement.clickNoWait();
                    }
                } else {
                    log.info("checkbox [{}] is not currently selected...", elementInfo);
                    if (check) {
                        log.info("checking checkbox [{}]...", elementInfo);
                        webElement.clickNoWait();
                    }
                }
            } else {
                throw new UnsupportedOperationException("not able to handle input type " + type);
            }
        } else if (tagName.equals("textarea")) {
            if (clear) {
                webElement.clear();
            }
            webElement.sendKeys(value.toString());
        } else if (tagName.equals("select")) {
            log.info("selecting value [{}] from select menu [{}]...", value.toString(), elementInfo);
            Select select = new Select(webElement);
            // TODO fix elementInfo displaying as obj ref for select
            select.selectByValue(value.toString());
        } else {
            throw new UnsupportedOperationException("not able to handle tag name " + tagName);
        }

    }
    return webElement;
}

From source file:org.qe4j.web.OpenWebDriver.java

License:Open Source License

/**
 * Finds and selects option by display text from specified select menu only
 * if the value specified is not null. In the case that the by argument is
 * ambiguous and finds more than one element, the first element is used.
 *
 * @param by//from  w  w w . java  2  s  .c o  m
 *            web driver identifier
 * @param text
 *            the display text of the option to select
 *
 * @return null if value is null, else the WebElement operated on
 */
public WebElement selectByText(By by, Object text) {
    if (text == null) {
        return null;
    }
    log.info("selecting option by visible text [{}] from select menu [{}]...", text.toString(), by.toString());

    List<WebElement> elements = findElements(by);
    if (elements.size() == 0) {
        throw new NoSuchElementException("input element not found with by: " + by.toString());
    }

    OpenWebElement webElement = (OpenWebElement) elements.get(0);

    // TODO fix elementInfo displaying as obj ref for select
    String elementInfo = OpenWebElement.extractElementInfo(webElement);

    log.info("select text [{}] from menu [{}]...", text.toString(), elementInfo);
    Select select = new Select(webElement);
    select.selectByVisibleText(text.toString());

    return webElement;
}

From source file:org.uiautomation.ios.selenium.Demo.java

License:Apache License

public static Callable<WebElement> elementToExist(final WebDriver driver, final By by) {
    return new Callable<WebElement>() {

        public WebElement call() throws Exception {
            return driver.findElement(by);
        }/*from  w  w w  .  j a  va 2 s.c om*/

        @Override
        public String toString() {
            return String.format("element with ID %s to exist", by.toString());
        }
    };
}

From source file:org.wso2.carbon.greg.publisher.utils.ESWebDriver.java

License:Open Source License

/**
 * This method wait for a given timeout and returns the WebElement
 *
 * @param by By element for findElement method
 * @return return the WebElement or null
 *///from ww  w  . j av  a 2s .c o  m
public WebElement findDynamicElement(By by, int timeOut) {
    WebDriverWait wait = new WebDriverWait(driver, timeOut);
    try {
        return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (TimeoutException ex) {
        log.error("Element not found : " + by.toString() + " in given timeout", ex);
    }

    return null;
}

From source file:org.wso2.iot.integration.ui.pages.UIUtils.java

License:Open Source License

public static boolean isElementPresent(Log log, WebDriver driver, By by) {
    try {//  ww w  .ja  v  a 2 s . c om
        WebDriverWait wait = new WebDriverWait(driver, webDriverTime);
        wait.until(ExpectedConditions.presenceOfElementLocated(by));
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        log.error(by.toString() + " is not present");
        return false;
    }

}

From source file:org.xframium.device.factory.DeviceWebDriver.java

License:Open Source License

public List<WebElement> findElements(By by) {
    if (cachingEnabled && cachedDocument == null)
        cacheData();//from  w  ww.j  a  v  a 2 s .  c  o  m

    if (cachingEnabled && cachedDocument != null) {
        try {
            XPath xPath = xPathFactory.newXPath();
            String path = by.toString();
            path = path.substring(path.indexOf(": ") + 2);
            NodeList nodes = (NodeList) xPath.evaluate(path, cachedDocument, XPathConstants.NODESET);

            List<WebElement> elementList = new ArrayList<WebElement>(10);

            for (int i = 0; i < nodes.getLength(); i++)
                elementList.add(new CachedWebElement(this, webDriver, by, nodes.item(i)));

            return elementList;
        } catch (Exception e) {
            log.warn("Error reading from cache " + e.getMessage());
            cachingEnabled = false;
            cachedDocument = null;
        }
    }

    return webDriver.findElements(by);
}

From source file:org.xframium.device.factory.DeviceWebDriver.java

License:Open Source License

public WebElement findElement(By by) {
    if (by instanceof ByXPath) {
        if (cachingEnabled && cachedDocument == null)
            cacheData();/*w  ww . j  a  va 2  s . com*/

        if (cachingEnabled && cachedDocument != null) {
            try {
                XPath xPath = xPathFactory.newXPath();
                String path = by.toString();
                path = path.substring(path.indexOf(": ") + 2);
                Node node = (Node) xPath.evaluate(path, cachedDocument, XPathConstants.NODE);

                if (node != null)
                    return new CachedWebElement(this, webDriver, by, node);
                else
                    cachedDocument = null;
            } catch (Exception e) {
                log.warn("Error reading from cache ", e);
                cachingEnabled = false;
                cachedDocument = null;
            }
        }
    }
    return new MorelandWebElement(this, webDriver.findElement(by));
}