Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlElement#getScriptableObject()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlElement#getScriptableObject() . 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
/**
 * Returns this document's {@code body} element.
 * @return this document's {@code body} element
 */
@JsxGetter({CHROME, IE})
public HTMLElement getBody() {
    final Page page = getPage();
    if (page instanceof HtmlPage) {
        final HtmlElement body = ((HtmlPage) page).getBody();
        if (body != null) {
            return body.getScriptableObject();
        }
    }
    return null;
}
 
Example 3
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public HTMLElement getHead() {
    final HtmlElement head = getPage().getHead();
    if (head == null) {
        return null;
    }
    return head.getScriptableObject();
}
 
Example 4
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 5
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns this document's {@code body} element.
 * @return this document's {@code body} element
 */
@JsxGetter({CHROME, IE, EDGE})
@CanSetReadOnly(CanSetReadOnlyStatus.EXCEPTION)
public HTMLElement getBody() {
    final Page page = getPage();
    if (page instanceof HtmlPage) {
        final HtmlElement body = ((HtmlPage) page).getBody();
        if (body != null) {
            return (HTMLElement) body.getScriptableObject();
        }
    }
    return null;
}
 
Example 6
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public HTMLElement getHead() {
    final HtmlElement head = getPage().getHead();
    if (head != null) {
        return (HTMLElement) head.getScriptableObject();
    }
    return null;
}
 
Example 7
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object elementFromPoint(final int x, final int y) {
    final HtmlElement element = getPage().getElementFromPoint(x, y);
    return element == null ? null : element.getScriptableObject();
}
 
Example 8
Source File: HTMLObjectElementTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Simple hack to proof, that a test driver can manipulate
 * a activeX mock at runtime.
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {},
        IE = {"Javascript called this method!", "ActiveX is still alive"})
public void activeXInteraction() throws Exception {
    final String clsid = "clsid:TESTING-CLASS-ID";
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    var obj = document.all.id1;\n"
        + "    if (obj.GetMessage) {\n"
        + "      alert(obj.GetMessage());\n"
        + "    }\n"
        + "  }\n"
        + "</script>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <object id='id1' classid='" + clsid + "'></object>\n"
        + "  <button id='myButton' onClick='test()'>Click Me</button>\n"
        + "</body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final Map<String, String> activeXObjectMap = new HashMap<>();
    activeXObjectMap.put(clsid, "com.gargoylesoftware.htmlunit.javascript.MockActiveXObject");
    client.setActiveXObjectMap(activeXObjectMap);

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final HtmlPage page = loadPage(html);

    page.getHtmlElementById("myButton").click();

    final HtmlElement elem = page.getHtmlElementById("id1");
    final HTMLObjectElement jsElem = (HTMLObjectElement) elem.getScriptableObject();
    final MockActiveXObject activeX = (MockActiveXObject) jsElem.unwrap();
    if (null != activeX) {
        activeX.setMessage("ActiveX is still alive");
        page.getHtmlElementById("myButton").click();
    }

    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 9
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object elementFromPoint(final int x, final int y) {
    final HtmlElement element = getPage().getElementFromPoint(x, y);
    return element == null ? null : element.getScriptableObject();
}