com.gargoylesoftware.htmlunit.html.HtmlInlineFrame Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlInlineFrame. 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: HTMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the specified element as the document's active element.
 * @see HTMLElement#setActive()
 * @param element the new active element for this document
 */
public void setActiveElement(final HTMLElement element) {
    // TODO update page focus element also

    activeElement_ = element;

    if (element != null) {
        // if this is part of an iFrame, make the iFrame tag the
        // active element of his doc
        final WebWindow window = element.getDomNodeOrDie().getPage().getEnclosingWindow();
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            if (frame instanceof HtmlInlineFrame) {
                final Window winWithFrame = frame.getPage().getEnclosingWindow().getScriptableObject();
                ((HTMLDocument) winWithFrame.getDocument()).setActiveElement(
                            (HTMLElement) frame.getScriptableObject());
            }
        }
    }
}
 
Example #2
Source File: WebClient8Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if something goes wrong
 */
@Test
public void iFrameTextContent() throws Exception {
    final String html = "<html>\n"
            + "<head><title>foo</title></head>\n"
            + "<body>\n"
            + "  <iframe id='tester' src='second.html'></iframe>\n"
            + "</body></html>";

    final String plainContent = "plain frame content";

    try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
        final MockWebConnection webConnection = getMockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(plainContent, 200, "OK", MimeType.TEXT_PLAIN);
        webClient.setWebConnection(webConnection);

        final HtmlPage page = webClient.getPage(URL_FIRST);

        final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
        assertEquals(plainContent, ((TextPage) iFrame.getEnclosedWindow().getEnclosedPage()).getContent());
    }
}
 
Example #3
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the specified element as the document's active element.
 * @see HTMLElement#setActive()
 * @param element the new active element for this document
 */
public void setActiveElement(final HTMLElement element) {
    // TODO update page focus element also

    activeElement_ = element;

    if (element != null) {
        // if this is part of an iFrame, make the iFrame tag the
        // active element of his doc
        final WebWindow window = element.getDomNodeOrDie().getPage().getEnclosingWindow();
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            if (frame instanceof HtmlInlineFrame) {
                final Window winWithFrame = (Window) frame.getPage().getEnclosingWindow().getScriptableObject();
                ((HTMLDocument) winWithFrame.getDocument()).setActiveElement(
                            (HTMLElement) frame.getScriptableObject());
            }
        }
    }
}
 
Example #4
Source File: Node.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * If we have added a new iframe that
 * had no source attribute, we have to take care the
 * 'onload' handler is triggered.
 *
 * @param childDomNode
 */
private static void initInlineFrameIfNeeded(final DomNode childDomNode) {
    if (childDomNode instanceof HtmlInlineFrame) {
        final HtmlInlineFrame frame = (HtmlInlineFrame) childDomNode;
        if (DomElement.ATTRIBUTE_NOT_DEFINED == frame.getSrcAttribute()) {
            frame.loadInnerPage();
        }
    }
}
 
Example #5
Source File: WebClient3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that response stream can be read more than one time.
 * @throws Exception if an error occurs
 */
@Test
public void readStreamTwice() throws Exception {
    final String html = "<html>\n"
        + "<body>\n"
        + "<iframe src='binaryFile.bin'></iframe>\n"
        + "<iframe src='foo.html'></iframe>\n"
        + "</body></html>";

    final MockWebConnection mockConnection = getMockWebConnection();
    final byte[] binaryContent = new byte[4818];
    final Random random = new Random();
    for (int i = 0; i < binaryContent.length; i++) {
        binaryContent[i] = (byte) (random.nextInt(Byte.MAX_VALUE));
    }
    mockConnection.setDefaultResponse(binaryContent, 200, "OK", MimeType.APPLICATION_OCTET_STREAM);
    final URL urlFoo = new URL(URL_FIRST, "foo.html");
    mockConnection.setResponse(urlFoo, "<html></html>");

    final WebDriver driver = loadPage2(html);
    final WebElement iframe1 = driver.findElement(By.tagName("iframe"));
    if (driver instanceof HtmlUnitDriver) {
        final HtmlInlineFrame htmlUnitIFrame1 = (HtmlInlineFrame) toHtmlElement(iframe1);
        final WebResponse iframeWebResponse = htmlUnitIFrame1.getEnclosedPage().getWebResponse();
        byte[] receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        assertArrayEquals(binaryContent, receivedBytes);
    }
}
 
Example #6
Source File: HTMLDocumentWriteTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Regression test for Bug #71.
 * @throws Exception if the test fails
 */
@Test
public void write_script() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webClient.setWebConnection(webConnection);

    final String mainHtml
        = "<html><head><title>Main</title></head><body>\n"
        + "<iframe name='iframe' id='iframe' src='http://first'></iframe>\n"
        + "<script type='text/javascript'>\n"
        + "document.write('<script type=\"text/javascript\" src=\"http://script\"></' + 'script>');\n"
        + "</script></body></html>";
    webConnection.setResponse(new URL("http://main/"), mainHtml);

    final String firstHtml = "<html><body><h1 id='first'>First</h1></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><body><h1 id='second'>Second</h1></body></html>";
    webConnection.setResponse(URL_SECOND, secondHtml);

    final String script = "document.getElementById('iframe').src = '" + URL_SECOND + "';\n";
    webConnection.setResponse(new URL("http://script/"), script, MimeType.APPLICATION_JAVASCRIPT);

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

    final HtmlPage mainPage = webClient.getPage("http://main");
    assertEquals("Main", mainPage.getTitleText());

    final HtmlInlineFrame iFrame = mainPage.getHtmlElementById("iframe");

    assertEquals(URL_SECOND.toExternalForm(), iFrame.getSrcAttribute());

    final HtmlPage enclosedPage = (HtmlPage) iFrame.getEnclosedPage();
    // This will blow up if the script hasn't been written to the document
    // and executed so the second page has been loaded.
    enclosedPage.getHtmlElementById("second");
}
 
Example #7
Source File: HTMLIFrameElementTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void setSrcAttribute() throws Exception {
    final String firstContent
        = "<html><head><title>First</title><script>\n"
        + "  function test() {\n"
        + "    document.getElementById('iframe1').setAttribute('src', '" + URL_SECOND + "');\n"
        + "  }\n"
        + "</script></head>\n"
        + "<body onload='test()'>\n"
        + "<iframe id='iframe1'>\n"
        + "</body></html>";

    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    final String thirdContent = "<html><head><title>Third</title></head><body></body></html>";
    final WebClient client = getWebClient();

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webConnection.setResponse(URL_THIRD, thirdContent);

    client.setWebConnection(webConnection);

    final HtmlPage page = client.getPage(URL_FIRST);
    assertEquals("First", page.getTitleText());

    final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
    assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
    assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());

    iframe.setSrcAttribute(URL_THIRD.toExternalForm());
    assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
    assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
}
 
Example #8
Source File: WebClient8Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if something goes wrong
 */
@Test
public void iFrameInFragment() throws Exception {
    final String html = "<html>\n"
            + "<head><title>foo</title></head>\n"
            + "<body>\n"
            + "<p id='para'>hello</p>\n"
            + "</body></html>";

    final String html2 = "<html>\n"
            + "<head><title>frame</title></head>\n"
            + "<body>\n"
            + "</body></html>";

    final String fragment = "<iframe id='tester' src='second.html'></iframe>";

    try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
        final MockWebConnection webConnection = getMockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(html2);
        webClient.setWebConnection(webConnection);

        final HtmlPage page = webClient.getPage(URL_FIRST);
        final DomElement para = page.getElementById("para");
        page.getWebClient().getPageCreator().getHtmlParser().parseFragment(para, fragment);

        final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
        assertEquals("frame", ((HtmlPage) iFrame.getEnclosedWindow().getEnclosedPage()).getTitleText());
    }
}
 
Example #9
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * If we have added a new iframe that
 * had no source attribute, we have to take care the
 * 'onload' handler is triggered.
 *
 * @param childDomNode
 */
private static void initInlineFrameIfNeeded(final DomNode childDomNode) {
    if (childDomNode instanceof HtmlInlineFrame) {
        final HtmlInlineFrame frame = (HtmlInlineFrame) childDomNode;
        if (DomElement.ATTRIBUTE_NOT_DEFINED == frame.getSrcAttribute()) {
            frame.loadInnerPage();
        }
    }
}
 
Example #10
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 #11
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;
}