Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#getFirstByXPath()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getFirstByXPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the element with the specified ID, as long as it is an HTML element; {@code null} otherwise.
 * @param id the ID to search for
 * @return the element with the specified ID, as long as it is an HTML element; {@code null} otherwise
 */
@JsxFunction
public Object getElementById(final String id) {
    final DomNode domNode = getDomNodeOrDie();
    final Object domElement = domNode.getFirstByXPath("//*[@id = \"" + id + "\"]");
    if (domElement != null) {
        if (!(domNode instanceof XmlPage) || domElement instanceof HtmlElement
                || getBrowserVersion().hasFeature(JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT)) {
            return ((DomElement) domElement).getScriptableObject();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("getElementById(" + id + "): no HTML DOM node found with this ID");
        }
    }
    return null;
}
 
Example 2
Source File: XMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the element with the specified ID, as long as it is an HTML element; {@code null} otherwise.
 * @param id the ID to search for
 * @return the element with the specified ID, as long as it is an HTML element; {@code null} otherwise
 */
@JsxFunction
public Object getElementById(final String id) {
    final DomNode domNode = getDomNodeOrDie();
    final Object domElement = domNode.getFirstByXPath("//*[@id = \"" + id + "\"]");
    if (domElement != null) {
        if (!(domNode instanceof XmlPage) || domElement instanceof HtmlElement
                || getBrowserVersion().hasFeature(JS_XML_GET_ELEMENT_BY_ID__ANY_ELEMENT)) {
            return ((DomElement) domElement).getScriptableObject();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("getElementById(" + id + "): no HTML DOM node found with this ID");
        }
    }
    return null;
}
 
Example 3
Source File: JenkinsRule.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
public void assertXPathValue(DomNode page, String xpath, String expectedValue) {
    Object node = page.getFirstByXPath(xpath);
    assertNotNull("no node found", node);
    assertTrue("the found object was not a Node " + xpath, node instanceof org.w3c.dom.Node);

    org.w3c.dom.Node n = (org.w3c.dom.Node) node;
    String textString = n.getTextContent();
    assertEquals("xpath value should match for " + xpath, expectedValue, textString);
}
 
Example 4
Source File: JenkinsRule.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
public void assertXPathValueContains(DomNode page, String xpath, String needle) {
    Object node = page.getFirstByXPath(xpath);
    assertNotNull("no node found", node);
    assertTrue("the found object was not a Node " + xpath, node instanceof org.w3c.dom.Node);

    org.w3c.dom.Node n = (org.w3c.dom.Node) node;
    String textString = n.getTextContent();
    assertTrue("needle found in haystack", textString.contains(needle));
}
 
Example 5
Source File: HudsonTestCase.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
public void assertXPathValue(DomNode page, String xpath, String expectedValue) {
    Object node = page.getFirstByXPath(xpath);
    assertNotNull("no node found", node);
    assertTrue("the found object was not a Node " + xpath, node instanceof org.w3c.dom.Node);

    org.w3c.dom.Node n = (org.w3c.dom.Node) node;
    String textString = n.getTextContent();
    assertEquals("xpath value should match for " + xpath, expectedValue, textString);
}
 
Example 6
Source File: HudsonTestCase.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
public void assertXPathValueContains(DomNode page, String xpath, String needle) {
    Object node = page.getFirstByXPath(xpath);
    assertNotNull("no node found", node);
    assertTrue("the found object was not a Node " + xpath, node instanceof org.w3c.dom.Node);

    org.w3c.dom.Node n = (org.w3c.dom.Node) node;
    String textString = n.getTextContent();
    assertTrue("needle found in haystack", textString.contains(needle)); 
}