com.gargoylesoftware.htmlunit.html.HtmlDivision Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlDivision. 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: SgmlPageTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void getElementsByTagNameAsterisk() throws Exception {
    final String html
        = "<html><head><title>First</title></head>\n"
        + "<body>\n"
        + "<form><input type='button' name='button1' value='pushme'></form>\n"
        + "<div>a</div> <div>b</div> <div>c</div>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(html);

    final DomNodeList<DomElement> elements = page.getElementsByTagName("*");

    assertEquals(9, elements.getLength());
    validateDomNodeList(elements);

    final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
    page.getBody().appendChild(newDiv);
    assertEquals(10, elements.getLength());
    validateDomNodeList(elements);
}
 
Example #2
Source File: SgmlPageTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void getElementsByTagNameNSAsterisk() throws Exception {
    final String html
        = "<html><head><title>First</title></head>\n"
        + "<body>\n"
        + "<form><input type='button' name='button1' value='pushme'></form>\n"
        + "<div>a</div> <div>b</div> <div>c</div>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(html);

    final DomNodeList<DomElement> elements = page.getElementsByTagNameNS(Html.XHTML_NAMESPACE, "*");

    assertEquals(9, elements.getLength());
    validateDomNodeList(elements);

    final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
    page.getBody().appendChild(newDiv);
    assertEquals(10, elements.getLength());
    validateDomNodeList(elements);
}
 
Example #3
Source File: HTMLParserTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the new HTMLParser on a simple HTML string.
 * @throws Exception failure
 */
@Test
public void simpleHTMLString() throws Exception {
    final WebClient webClient = getWebClient();
    final WebResponse webResponse = new StringWebResponse(
        "<html><head><title>TITLE</title></head><body><div>TEST</div></body></html>", URL_FIRST);

    final HtmlPage page = webClient.getPageCreator().getHtmlParser()
                                        .parseHtml(webResponse, webClient.getCurrentWindow());

    final String stringVal = page.<HtmlDivision>getFirstByXPath("//div").getFirstChild().getNodeValue();
    assertEquals("TEST", stringVal);

    final HtmlElement node = (HtmlElement) page.getFirstByXPath("//*[./text() = 'TEST']");
    assertEquals(node.getTagName(), HtmlDivision.TAG_NAME);
}
 
Example #4
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if test fails
 */
@Test
public void changingAttributes() throws Exception {
    final String content = "<html><head><title>foo</title></head>\n"
        + "<body>\n"
        + "<div id='testDiv' title='foo'></div>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(content);
    final HtmlDivision div = page.getHtmlElementById("testDiv");

    assertSame(div, page.getFirstByXPath("//*[@title = 'foo']"));
    assertNull(page.getFirstByXPath("//*[@class = 'design']"));

    div.setAttribute("class", "design");
    assertSame(div, page.getFirstByXPath("//*[@class = 'design']"));
}
 
Example #5
Source File: GWT250Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void mail() throws Exception {
    final HtmlPage page = loadGWTPage("Mail", null, "//div[@class='MGJ']");
    assertSame(page.getEnclosingWindow(), page.getWebClient().getCurrentWindow());
    final HtmlDivision cell = page.getFirstByXPath("//div[@class='MGJ']");
    assertElementValue(cell, "Welcome back, [email protected]");

    final String[] selectedRow = {"markboland05", "[email protected]", "URGENT -[Mon, 24 Apr 2006 02:17:27 +0000]"};

    final List<?> selectedRowCells = page.getByXPath("//tr[@class='MKI']/td");
    assertEquals(selectedRow.length, selectedRowCells.size());
    for (int i = 0; i < selectedRow.length; i++) {
        final HtmlTableDataCell selectedRowCell = (HtmlTableDataCell) selectedRowCells.get(i);
        assertElementValue(selectedRowCell, selectedRow[i]);
    }

    verifyStartMailBody(page, "Dear Friend,",
            "I am Mr. Mark Boland the Bank Manager of ABN AMRO BANK 101 Moorgate, London, EC2M 6SB.");

    // click on email from Hollie Voss
    final HtmlElement elt = page.getFirstByXPath("//td[text() = 'Hollie Voss']");
    final HtmlPage page2 = elt.click();
    assertSame(page, page2);
    verifyStartMailBody(page, ">> Componentes e decodificadores; confira aqui;",
            "http://br.geocities.com/listajohn/index.htm",
            "THE GOVERNING AWARD");
}
 
Example #6
Source File: S2jhHtmlParseFilter.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isParseDataFetchLoaded(HtmlPage page) {
    HtmlDivision div = page.getFirstByXPath("//DIV[@id='description']/DIV[@class='content ke-post']");
    if (div != null && div.getChildElementCount() > 0) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Product description content HTML: {}", asString(div));
        }
        return true;
    }
    return false;
}
 
Example #7
Source File: ComputedCSSStyleDeclaration.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 * elements.
 *
 * @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 *         elements
 */
private int getEmptyHeight() {
    if (height2_ != null) {
        return height2_.intValue();
    }

    final DomNode node = getElement().getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    if (NONE.equals(getDisplay())) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    final Element elem = getElement();
    final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();

    if (elem instanceof HTMLBodyElement) {
        height2_ = windowHeight;
        return windowHeight;
    }

    final boolean explicitHeightSpecified = !super.getHeight().isEmpty();

    int defaultHeight;
    if (node instanceof HtmlDivision && StringUtils.isBlank(node.getTextContent())) {
        defaultHeight = 0;
    }
    else if (elem.getFirstChild() == null) {
        if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            defaultHeight = 13;
        }
        else if (node instanceof HtmlButton) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
            final BrowserVersion browser = getBrowserVersion();
            if (browser.hasFeature(JS_CLIENTHIGHT_INPUT_17)) {
                defaultHeight = 17;
            }
            else if (browser.hasFeature(JS_CLIENTHIGHT_INPUT_21)) {
                defaultHeight = 21;
            }
            else {
                defaultHeight = 20;
            }
        }
        else if (node instanceof HtmlSelect) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlTextArea) {
            defaultHeight = 49;
        }
        else if (node instanceof HtmlInlineFrame) {
            defaultHeight = 154;
        }
        else {
            defaultHeight = 0;
        }
    }
    else {
        defaultHeight = getBrowserVersion().getFontHeight(getFontSize());
        if (node instanceof HtmlDivision) {
            defaultHeight *= StringUtils.countMatches(node.asText(), '\n') + 1;
        }
    }

    final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;

    int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
        @Override public String get(final ComputedCSSStyleDeclaration style) {
            final Element element = style.getElement();
            if (element instanceof HTMLBodyElement) {
                return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
            }
            return style.getStyleAttribute(HEIGHT, true);
        }
    });

    if (height == 0 && !explicitHeightSpecified) {
        height = defaultHeight;
    }

    height2_ = Integer.valueOf(height);
    return height;
}
 
Example #8
Source File: WindowConcurrencyTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Regression test for
 * <a href="http://sourceforge.net/support/tracker.php?aid=2820051">bug 2820051</a>.
 * @throws Exception if the test fails
 */
@Test
public void concurrentModificationException_computedStyles() throws Exception {
    final String html
        = "<html><head><script>\n"
        + "function test() {\n"
        + "  getComputedStyle(document.body, null);\n"
        + "}\n"
        + "</script></head><body onload='test()'>\n"
        + "<iframe src='foo.html' name='myFrame' id='myFrame'></iframe>\n"
        + "</body></html>";

    final String html2 = "<html><head><script>\n"
        + "function forceStyleComputationInParent() {\n"
        + "  var newNode = parent.document.createElement('span');\n"
        + "  parent.document.body.appendChild(newNode);\n"
        + "  parent.getComputedStyle(newNode, null);\n"
        + "}\n"
        + "setInterval(forceStyleComputationInParent, 10);\n"
        + "</script></head></body></html>";

    try (WebClient client = new WebClient(BrowserVersion.FIREFOX_60)) {
        final MockWebConnection webConnection = new MockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(html2);
        client.setWebConnection(webConnection);

        final HtmlPage page1 = client.getPage(URL_FIRST);

        // Recreating what can occur with two threads requires
        // to know a bit about the style invalidation used in Window.DomHtmlAttributeChangeListenerImpl
        final HtmlElement elt = new HtmlDivision("div", page1, new HashMap<String, DomAttr>()) {
            @Override
            public DomNode getParentNode() {
                // this gets called by CSS invalidation logic
                try {
                    Thread.sleep(1000); // enough to let setInterval run
                }
                catch (final InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return super.getParentNode();
            }
        };
        page1.getBody().appendChild(elt);
    }
}
 
Example #9
Source File: ComputedCSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 * elements.
 *
 * @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 *         elements
 */
private int getEmptyHeight() {
    if (height2_ != null) {
        return height2_.intValue();
    }

    final DomNode node = getElement().getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    if ("none".equals(getDisplay())) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    final Element elem = getElement();
    final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();

    if (elem instanceof HTMLBodyElement) {
        height2_ = windowHeight;
        return windowHeight;
    }

    final boolean explicitHeightSpecified = !super.getHeight().isEmpty();

    int defaultHeight;
    if (node instanceof HtmlDivision && StringUtils.isBlank(node.getTextContent())) {
        defaultHeight = 0;
    }
    else if (elem.getFirstChild() == null) {
        if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            defaultHeight = 13;
        }
        else if (node instanceof HtmlButton) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
            if (getBrowserVersion().hasFeature(JS_CLIENTHIGHT_INPUT_17)) {
                defaultHeight = 17;
            }
            else {
                defaultHeight = 21;
            }
        }
        else if (node instanceof HtmlSelect) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlTextArea) {
            defaultHeight = 49;
        }
        else if (node instanceof HtmlInlineFrame) {
            defaultHeight = 154;
        }
        else {
            defaultHeight = 0;
        }
    }
    else {
        defaultHeight = getBrowserVersion().getFontHeight(getFontSize());
        if (node instanceof HtmlDivision) {
            defaultHeight *= StringUtils.countMatches(node.asText(), '\n') + 1;
        }
    }

    final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;

    int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
        @Override public String get(final ComputedCSSStyleDeclaration style) {
            final Element element = style.getElement();
            if (element instanceof HTMLBodyElement) {
                return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
            }
            return style.getStyleAttribute(HEIGHT, true);
        }
    });

    if (height == 0 && !explicitHeightSpecified) {
        height = defaultHeight;
    }

    height2_ = Integer.valueOf(height);
    return height;
}
 
Example #10
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * @param page the page
 * @param target the target
 * @param append append or no
 */
public ProxyDomNode(final SgmlPage page, final DomNode target, final boolean append) {
    super(HtmlDivision.TAG_NAME, page, null);
    target_ = target;
    append_ = append;
}
 
Example #11
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * @param page the page
 * @param target the target
 * @param append append or no
 */
public ProxyDomNode(final SgmlPage page, final DomNode target, final boolean append) {
    super(HtmlDivision.TAG_NAME, page, null);
    target_ = target;
    append_ = append;
}