Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getBody()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getBody() . 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: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code onload} property. Note that this is not necessarily a function if something else has been set.
 * @return the {@code onload} property
 */
@JsxGetter
public Object getOnload() {
    final Object onload = getEventHandler(Event.TYPE_LOAD);
    if (onload == null) {
        final HtmlPage page = (HtmlPage) getWebWindow().getEnclosedPage();
        final HtmlElement body = page.getBody();
        if (body != null) {
            final HTMLBodyElement b = body.getScriptableObject();
            return b.getEventHandler("onload");
        }
        return null;
    }
    return onload;
}
 
Example 2
Source File: Document.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a value which indicates whether or not the document can be edited.
 * @param mode a value which indicates whether or not the document can be edited
 */
@JsxSetter({CHROME, IE})
public void setDesignMode(final String mode) {
    final BrowserVersion browserVersion = getBrowserVersion();
    final boolean inherit = browserVersion.hasFeature(JS_DOCUMENT_DESIGN_MODE_INHERIT);
    if (inherit) {
        if (!"on".equalsIgnoreCase(mode) && !"off".equalsIgnoreCase(mode) && !"inherit".equalsIgnoreCase(mode)) {
            throw Context.reportRuntimeError("Invalid document.designMode value '" + mode + "'.");
        }

        if ("on".equalsIgnoreCase(mode)) {
            designMode_ = "on";
        }
        else if ("off".equalsIgnoreCase(mode)) {
            designMode_ = "off";
        }
        else if ("inherit".equalsIgnoreCase(mode)) {
            designMode_ = "inherit";
        }
    }
    else {
        if ("on".equalsIgnoreCase(mode)) {
            designMode_ = "on";
            final SgmlPage page = getPage();
            if (page != null && page.isHtmlPage()
                    && getBrowserVersion().hasFeature(JS_DOCUMENT_SELECTION_RANGE_COUNT)) {
                final HtmlPage htmlPage = (HtmlPage) page;
                final DomNode child = htmlPage.getBody().getFirstChild();
                final DomNode rangeNode = child == null ? htmlPage.getBody() : child;
                htmlPage.setSelectionRange(new SimpleRange(rangeNode, 0));
            }
        }
        else if ("off".equalsIgnoreCase(mode)) {
            designMode_ = "off";
        }
    }
}
 
Example 3
Source File: CSSStyleSheet2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void selects_miscSelectors() throws Exception {
    final String html = "<html><head><title>test</title>\n"
        + "</head><body><style></style>\n"
        + "<form name='f1' action='foo' class='yui-log'>\n"
        + "<div><div><input name='i1' id='m1'></div></div>\n"
        + "<input name='i2' class='yui-log'>\n"
        + "<button name='b1' class='yui-log'>\n"
        + "<button name='b2'>\n"
        + "</form>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(html);
    final HtmlElement body = page.getBody();
    final HtmlForm form = page.getFormByName("f1");
    final HtmlInput input1 = (HtmlInput) page.getElementsByName("i1").get(0);
    final HtmlInput input2 = (HtmlInput) page.getElementsByName("i2").get(0);
    final DomElement button1 = page.getElementsByName("b1").get(0);
    final DomElement button2 = page.getElementsByName("b2").get(0);
    final BrowserVersion browserVersion = getBrowserVersion();

    final HtmlStyle node = (HtmlStyle) page.getElementsByTagName("style").item(0);
    final HTMLStyleElement host = (HTMLStyleElement) node.getScriptableObject();
    final CSSStyleSheet sheet = host.getSheet();

    Selector selector = parseSelector(sheet, "*.yui-log input");

    assertFalse(CSSStyleSheet.selects(browserVersion, selector, body, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, form, null, false));
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input1, null, false));
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input2, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, button1, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, button2, null, false));

    selector = parseSelector(sheet, "#m1");
    assertTrue(CSSStyleSheet.selects(browserVersion, selector, input1, null, false));
    assertFalse(CSSStyleSheet.selects(browserVersion, selector, input2, null, false));
}
 
Example 4
Source File: XMLHttpRequest3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Regression test for bug 1209686 (onreadystatechange not called with partial data when emulating FF).
 * @throws Exception if an error occurs
 */
@Test
@NotYetImplemented
public void streaming() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/test", StreamingServlet.class);

    final String resourceBase = "./src/test/resources/com/gargoylesoftware/htmlunit/javascript/host";
    startWebServer(resourceBase, null, servlets);
    final WebClient client = getWebClient();
    final HtmlPage page = client.getPage(URL_FIRST + "XMLHttpRequestTest_streaming.html");
    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
    final HtmlElement body = page.getBody();
    assertEquals(10, body.asText().split("\n").length);
}
 
Example 5
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code onload} property. Note that this is not necessarily a function if something else has been set.
 * @return the {@code onload} property
 */
@JsxGetter
public Object getOnload() {
    final Object onload = getEventHandler(Event.TYPE_LOAD);
    if (onload == null) {
        final HtmlPage page = (HtmlPage) getWebWindow().getEnclosedPage();
        final HtmlElement body = page.getBody();
        if (body != null) {
            final HTMLBodyElement b = (HTMLBodyElement) body.getScriptableObject();
            return b.getEventHandler("onload");
        }
        return null;
    }
    return onload;
}
 
Example 6
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a value which indicates whether or not the document can be edited.
 * @param mode a value which indicates whether or not the document can be edited
 */
@JsxSetter({CHROME, IE})
public void setDesignMode(final String mode) {
    final BrowserVersion browserVersion = getBrowserVersion();
    final boolean inherit = browserVersion.hasFeature(JS_DOCUMENT_DESIGN_MODE_INHERIT);
    if (inherit) {
        if (!"on".equalsIgnoreCase(mode) && !"off".equalsIgnoreCase(mode) && !"inherit".equalsIgnoreCase(mode)) {
            throw Context.reportRuntimeError("Invalid document.designMode value '" + mode + "'.");
        }

        if ("on".equalsIgnoreCase(mode)) {
            designMode_ = "on";
        }
        else if ("off".equalsIgnoreCase(mode)) {
            designMode_ = "off";
        }
        else if ("inherit".equalsIgnoreCase(mode)) {
            designMode_ = "inherit";
        }
    }
    else {
        if ("on".equalsIgnoreCase(mode)) {
            designMode_ = "on";
            final SgmlPage page = getPage();
            if (page != null && page.isHtmlPage()
                    && getBrowserVersion().hasFeature(JS_DOCUMENT_SELECTION_RANGE_COUNT)) {
                final HtmlPage htmlPage = (HtmlPage) page;
                final DomNode child = htmlPage.getBody().getFirstChild();
                final DomNode rangeNode = child == null ? htmlPage.getBody() : child;
                htmlPage.setSelectionRange(new SimpleRange(rangeNode, 0));
            }
        }
        else if ("off".equalsIgnoreCase(mode)) {
            designMode_ = "off";
        }
    }
}