Example usage for org.openqa.selenium WebElement getTagName

List of usage examples for org.openqa.selenium WebElement getTagName

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getTagName.

Prototype

String getTagName();

Source Link

Document

Get the tag name of this element.

Usage

From source file:org.safs.selenium.webdriver.lib.SearchObject.java

License:Open Source License

/**
 * Return a string to describe a selenium web element.<br>
 * @param element WebElement/*from  www .ja va  2s  . c  o  m*/
 * @return String,
 */
static String getDescription(WebElement element) {
    StringBuffer buffer = new StringBuffer();

    if (element != null) {
        buffer.append("<" + element.getTagName());
        String clazz = element.getAttribute(Component.ATTRIBUTE_CLASS);
        if (clazz != null && !clazz.isEmpty()) {
            buffer.append(" class='" + clazz + "'");
        }
        buffer.append("/>");
    }

    return buffer.toString();
}

From source file:org.safs.selenium.webdriver.lib.SearchObject.java

License:Open Source License

/**
 * Return the parent WebElement of the provided WebElement.<br>
 * @param element WebElement//  w  ww  . jav  a  2s.c o  m
 * @return WebElement parent or null if there is no parent, or an error occurred.
 */
public static WebElement getParentWebElement(WebElement element) {
    WebElement e = null;
    if (element == null)
        return null;
    try {
        // TODO: what if the html is in a frame?  What do we see?
        if (element.getTagName().equalsIgnoreCase(HTML.TAG_HTML))
            return null;
        e = element.findElement(By.xpath(".."));
    } catch (Exception ignore) {
    }
    return e;
}

From source file:org.safs.selenium.webdriver.lib.SearchObject.java

License:Open Source License

/**
 * Attempt to generate a simple Xpath string for this one element without any parent
 * path information./*w  ww.j a v a  2  s .c  o m*/
 * <p>
 * Currently tries to differentiate the tag name by id, title, or class.
 * <p>
 * Examples:
 * <p><ul>
 * div[@id='myid']<br>
 * div[@title='mytitle']<br>
 * div[@class='myclass']<br>
 * </ul>
 * <p>
 * if none of those have valid values, then only the tag name is returned. In that case,
 * we check to see if it is the Nth sibling of the same tag type and add that, if needed.
 *
 * @param element
 * @return
 * @author CANAGL adding support for 'name' attribute if no other attribs were available
 */
public static String generateGenericXPath(WebElement element) {
    String tag = element.getTagName();
    String attID = element.getAttribute("id");
    String attTITLE = element.getAttribute("title");
    String attCLASS = element.getAttribute("class");
    String att = (attID != null) && (attID.length() > 0) ? "[@id='" + attID + "']"
            : (attTITLE != null && attTITLE.length() > 0) ? "[@title='" + attTITLE + "']"
                    : (attCLASS != null && attCLASS.length() > 0) ? "[@class='" + attCLASS + "']" : "";
    if (att.length() == 0) {
        attID = element.getAttribute("name");
        if (attID != null && attID.length() > 0)
            att = "[@name='" + attID + "']";
    }
    List sibs = element.findElements(By.xpath("preceding-sibling::" + tag + att));
    if (!sibs.isEmpty()) {
        att += "[" + String.valueOf(sibs.size() + 1) + "]";
    }
    IndependantLog.info("SearchObject.generateGenericXPath made " + tag + att);
    return tag + att;
}

From source file:org.safs.selenium.webdriver.WebDriverGUIUtilities.java

License:Open Source License

/**
 * Gets the object defined by the windowName and childName
 * @param mapName  the name/ID of the App Map currently in use.
 * @param windowName  the name/ID of the window as predefined in the App Map.
 * @param childName    the name/ID of the child component of the window as predefined in the App Map.
 * @param ignoreCache if true, try getting the component from the appmap
 * @return the object defined by the windowName and childName
 *///w  w  w. ja  v  a 2 s. c o  m
public WebElement getTestObject(String mapname, String windowName, String childName, boolean ignoreCache) {
    if (mapname == null || windowName == null)
        return null;
    ApplicationMap map = getAppMap(mapname);

    if (map == null) {
        if (registerAppMap(mapname, mapname)) {
            map = getAppMap(mapname);
            if (map == null) {
                Log.info("WDDDG: gto1 could NOT retrieve registered AppMap " + mapname);
                return null;
            }
        }
        // what if NOT registered?
        else {
            Log.info("WDDDG: gto1 could NOT register AppMap " + mapname);
            return null;
        }
    }

    WebElement tobj = null;
    if ((childName == null) || (windowName.equalsIgnoreCase(childName))) {

        if (!ignoreCache) {
            tobj = (WebElement) map.getParentObject(windowName);
        }
        if (tobj == null) {
            //map.setParentObject(windowName, null);
            try {
                waitForObject(mapname, windowName, windowName, gSecTimeout);
            } catch (SAFSException e) {
                Log.info("WDDDG: Could not waitForObject " + windowName);
                return null;
            }
            tobj = (WebElement) map.getParentObject(windowName);
            if (tobj == null) {
                Log.info("WDDDG: Could not waitForObject " + windowName);
                return null;
            }
        } else {
            return tobj;
        }

    } else {

        if (!ignoreCache) {
            tobj = (WebElement) map.getChildObject(windowName, childName);
        }
        if (tobj == null) {
            //map.setChildObject(windowName, childName, null);
            try {
                waitForObject(mapname, windowName, childName, gSecTimeout);
            } catch (SAFSException e) {
                Log.info("WDDDG: Could not waitForObject " + childName);
                return null;
            }
            tobj = (WebElement) map.getChildObject(windowName, childName);
            if (tobj == null) {
                Log.info("WDDDG: Could not waitForObject " + childName);
                return null;
            }
            ((WDTestRecordHelper) trdata).setCompTestObject(tobj);
        } else {
            Log.info("WDDDG: returning cached object: " + tobj.getTagName());
            return tobj;
        }
    }

    try {
        Log.info("WDDDG: compTestObject : " + tobj.getTagName());
    } catch (Exception npe2) {
        Log.info("WDDDG: No Mapped TestObject named \"" + childName + "\" found.");
    }

    return tobj;
}

From source file:org.specrunner.webdriver.AbstractPluginFind.java

License:Open Source License

/**
 * Show the element as Strings.//w  ww. j a  v  a2 s .  c om
 * 
 * @param element
 *            The element.
 * @return A string representation.
 */
public String asString(WebElement element) {
    if (UtilLog.LOG.isDebugEnabled()) {
        try {
            return element.getTagName() + "." + getText(element);
        } catch (Exception e) {
            if (UtilLog.LOG.isTraceEnabled()) {
                UtilLog.LOG.trace(e.getMessage(), e);
            }
            try {
                return element.getText();
            } catch (Exception e1) {
                if (UtilLog.LOG.isTraceEnabled()) {
                    UtilLog.LOG.trace(e1.getMessage(), e1);
                }
            }
        }
    }
    return String.valueOf(element);
}

From source file:org.specrunner.webdriver.AbstractPluginFind.java

License:Open Source License

/**
 * Get the value of element. Depends on element type.
 * //from  w  w  w .  j av  a2s .  co m
 * @param element
 *            The element.
 * @return The value.
 */
protected String getText(WebElement element) {
    String tagName = element.getTagName().toLowerCase();
    boolean isText = !("input".equals(tagName) || "textarea".equals(tagName));
    return isText ? element.getText() : element.getAttribute("value");
}

From source file:org.specrunner.webdriver.actions.AbstractPluginCheck.java

License:Open Source License

/**
 * Check if is checkbox./*from w w  w . j  a va  2s .c o m*/
 * 
 * @param element
 *            The element.
 * @return true, if checkbox, false, otherwise.
 */
protected boolean isCheckbox(WebElement element) {
    return "input".equals(element.getTagName()) && "checkbox".equals(element.getAttribute("type"));
}

From source file:org.specrunner.webdriver.actions.AbstractPluginSelect.java

License:Open Source License

/**
 * Says if the element is a select.// w ww  . j  av  a 2  s  .  com
 * 
 * @param element
 *            The element.
 * @return true, if is select, false, otherwise.
 */
protected boolean isSelect(WebElement element) {
    return element.getTagName().equalsIgnoreCase("select");
}

From source file:org.specrunner.webdriver.assertions.AbstractPluginCheckable.java

License:Open Source License

@Override
protected void process(IContext context, IResultSet result, WebDriver client, WebElement[] elements)
        throws PluginException {
    boolean error = false;
    for (WebElement element : elements) {
        if (isCheckbox(element) || isRadio(element)) {
            boolean componentStatus = element.isSelected();
            if (expected() != componentStatus) {
                result.addResult(Failure.INSTANCE, context.peek(),
                        new PluginException("Element " + getFinderInstance().resume(context) + " should be '"
                                + (expected() ? "checked" : "unchecked") + "' but is '"
                                + (componentStatus ? "checked" : "unchecked") + "'."),
                        SRServices.get(IWritableFactoryManager.class).get(WebDriver.class).newWritable(client));
                error = true;/*  w  w  w. j a v  a 2s . c o  m*/
            }
        } else {
            result.addResult(Failure.INSTANCE, context.peek(),
                    new PluginException("Element " + getFinderInstance().resume(context)
                            + " is not a checkbox or radio is " + element.getTagName()),
                    SRServices.get(IWritableFactoryManager.class).get(WebDriver.class).newWritable(client));
            error = true;
        }
    }
    if (!error) {
        result.addResult(Success.INSTANCE, context.peek());
    }
}

From source file:org.specrunner.webdriver.assertions.AbstractPluginCheckable.java

License:Open Source License

/**
 * Return if a element is a input type=radio.
 * /*from  www.j a v a  2  s .c o m*/
 * @param element
 *            The element.
 * @return true, if radio, false otherwise.
 */
protected boolean isRadio(WebElement element) {
    return "input".equals(element.getTagName()) && "radio".equals(element.getAttribute("type"));
}