Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#getWebWindowByName()

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#getWebWindowByName() . 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: HTMLDocumentWriteTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void aboutURL() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent =
        "<html><body><script language='JavaScript'>\n"
        + "w2 = window.open('about:blank', 'AboutBlank');\n"
        + "w2.document.open();\n"
        + "w2.document.write('<html><head><title>hello</title></head><body></body></html>');\n"
        + "w2.document.close();\n"
        + "</script></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webClient.setWebConnection(webConnection);

    webClient.getPage(URL_FIRST);
    final WebWindow webWindow = webClient.getWebWindowByName("AboutBlank");
    assertNotNull(webWindow);

    //  final HtmlPage page = (HtmlPage) webWindow.getEnclosedPage();
    //  assertEquals("<html><head><title>hello</title></head><body></body></html>",page.getDocument().toString());
}
 
Example 2
Source File: HtmlFrameSetTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void loadingIFrames() throws Exception {
    final String firstContent
        = "<html><head><title>First</title></head>\n"
        + "<body>\n"
        + "  <iframe name='left' src='" + URL_SECOND + "' />\n"
        + "  some stuff"
        + "</html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient webClient = getWebClientWithMockWebConnection();

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

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
    assertTrue(FrameWindow.class.isInstance(secondWebWindow));
    assertSame(firstPage, ((FrameWindow) secondWebWindow).getEnclosingPage());
    assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
}
 
Example 3
Source File: HtmlFrameSetTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void loadingFrameSet() throws Exception {
    final String firstContent
        = "<html><head><title>First</title></head>\n"
        + "<frameset cols='130,*'>\n"
        + "  <frame scrolling='no' name='left' src='" + URL_SECOND + "' frameborder='1' />\n"
        + "  <frame scrolling='auto' name='right' src='" + URL_THIRD + "' frameborder='1' />\n"
        + "  <noframes>\n"
        + "    <body>Frames not supported</body>\n"
        + "  </noframes>\n"
        + "</frameset>\n"
        + "</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 webClient = getWebClientWithMockWebConnection();

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

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
    assertSame(firstPage, ((FrameWindow) secondWebWindow).getEnclosingPage());
    assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());

    final WebWindow thirdWebWindow = webClient.getWebWindowByName("right");
    assertTrue(FrameWindow.class.isInstance(thirdWebWindow));
    assertSame(firstPage, ((FrameWindow) thirdWebWindow).getEnclosingPage());
    assertEquals("Third", ((HtmlPage) thirdWebWindow.getEnclosedPage()).getTitleText());
}
 
Example 4
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Opens a new window.
 *
 * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
 *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
 * @param name the name
 * @param features the features
 * @param replace whether to replace in the history list or no
 * @return the newly opened window, or {@code null} if popup windows have been disabled
 * @see com.gargoylesoftware.htmlunit.WebClientOptions#isPopupBlockerEnabled()
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536651.aspx">MSDN documentation</a>
 */
@JsxFunction
public WindowProxy open(final Object url, final Object name, final Object features,
        final Object replace) {
    String urlString = null;
    if (!Undefined.isUndefined(url)) {
        urlString = Context.toString(url);
    }
    String windowName = "";
    if (!Undefined.isUndefined(name)) {
        windowName = Context.toString(name);
    }
    String featuresString = null;
    if (!Undefined.isUndefined(features)) {
        featuresString = Context.toString(features);
    }
    final WebClient webClient = getWebWindow().getWebClient();

    if (webClient.getOptions().isPopupBlockerEnabled()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Ignoring window.open() invocation because popups are blocked.");
        }
        return null;
    }

    boolean replaceCurrentEntryInBrowsingHistory = false;
    if (!Undefined.isUndefined(replace)) {
        replaceCurrentEntryInBrowsingHistory = Context.toBoolean(replace);
    }
    if ((featuresString != null || replaceCurrentEntryInBrowsingHistory) && LOG.isDebugEnabled()) {
        LOG.debug(
               "window.open: features and replaceCurrentEntryInBrowsingHistory "
                + "not implemented: url=[" + urlString
                + "] windowName=[" + windowName
                + "] features=[" + featuresString
                + "] replaceCurrentEntry=[" + replaceCurrentEntryInBrowsingHistory
                + "]");
    }

    // if specified name is the name of an existing window, then hold it
    if (StringUtils.isEmpty(urlString) && !"".equals(windowName)) {
        try {
            final WebWindow webWindow = webClient.getWebWindowByName(windowName);
            return getProxy(webWindow);
        }
        catch (final WebWindowNotFoundException e) {
            // nothing
        }
    }
    final URL newUrl = makeUrlForOpenWindow(urlString);
    final WebWindow newWebWindow = webClient.openWindow(newUrl, windowName, webWindow_);
    return getProxy(newWebWindow);
}
 
Example 5
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void parentAndTop() throws Exception {
    final String firstContent
        = "<html><head><title>First</title></head><body>\n"
        + "  <iframe name='left' src='" + URL_SECOND + "'></iframe>\n"
        + "</body></html>";
    final String secondContent
        = "<html><head><title>Second</title></head><body>\n"
        + "  <iframe name='innermost' src='" + URL_THIRD + "'></iframe>\n"
        + "</body></html>";
    final String thirdContent
        = "<html><head><title>Third</title><script>\n"
        + "function doAlert() {\n"
        + "  alert(parent != this);\n"
        + "  alert(top != this);\n"
        + "  alert(parent != top);\n"
        + "  alert(parent.parent == top);\n"
        + "  alert(parent.frames[0] == this);\n"
        + "  alert(top.frames[0] == parent);\n"
        + "}\n"
        + "</script></head>\n"
        + "<body><a id='clickme' onClick='doAlert()'>foo</a></body></html>";

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

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

    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final WebWindow innermostWebWindow = webClient.getWebWindowByName("innermost");
    final HtmlPage innermostPage = (HtmlPage) innermostWebWindow.getEnclosedPage();
    innermostPage.getHtmlElementById("clickme").click();

    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getTopWindow(), innermostWebWindow);
    assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow.getTopWindow());
    assertSame(innermostWebWindow.getParentWindow().getParentWindow(), innermostWebWindow.getTopWindow());

    assertEquals(new String[] {"true", "true", "true", "true", "true", "true"}, collectedAlerts);
}
 
Example 6
Source File: HtmlFrameSetTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Regression test for Bug #203.
 * @throws Exception if the test fails
 */
@Test
public void loadingFrameSetWithRelativePaths() throws Exception {
    final String framesContent
        = "<html><head><title>Frames</title></head>\n"
        + "<frameset rows='110,*'>\n"
        + "  <frame src='subdir1/menu.html' name='menu' scrolling='no' border='0' noresize>\n"
        + "  <frame src='subdir2/first.html' name='test' border='0' auto>\n"
        + "</frameset>\n"
        + "<noframes>\n"
        + "  <body>Frames not supported</body>\n"
        + "</noframes>\n"
        + "</html>";
    final String menuContent
        = "<html><head><title>Menu</title></head>\n"
        + "<body>\n"
        + "  <script language='javascript'>\n"
        + "    function changeEditPage() {parent.test.location='../second.html';}\n"
        + "  </script>\n"
        + "  <a name ='changePage' onClick='javascript:changeEditPage();' href='#'>Click</a>."
        + "</body>\n"
        + "</html>";
    final String firstContent
        = "<html><head><title>First</title></head>\n"
        + "<body>First/body>\n"
        + "</html>";
    final String secondContent
        = "<html><head><title>Second</title></head>\n"
        + "<body>Second</body>\n"
        + "</html>";
    final String baseUrl = "http://framestest";

    final URL framesURL = new URL(baseUrl + "/frames.html");
    final URL menuURL = new URL(baseUrl + "/subdir1/menu.html");
    final URL firstURL = new URL(baseUrl + "/subdir2/first.html");
    final URL secondURL = new URL(baseUrl + "/second.html");

    final WebClient webClient = getWebClientWithMockWebConnection();

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(framesURL, framesContent);
    webConnection.setResponse(menuURL, menuContent);
    webConnection.setResponse(firstURL, firstContent);
    webConnection.setResponse(secondURL, secondContent);

    final HtmlPage framesPage = webClient.getPage(framesURL);
    assertEquals("Frames", framesPage.getTitleText());

    final WebWindow menuWebWindow = webClient.getWebWindowByName("menu");
    final HtmlPage menuPage = (HtmlPage) menuWebWindow.getEnclosedPage();
    assertEquals("Menu", menuPage.getTitleText());

    final WebWindow testWebWindow = webClient.getWebWindowByName("test");
    assertEquals("First", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText());

    final HtmlAnchor changePage = menuPage.getAnchorByName("changePage");
    changePage.click();
    assertEquals("Second", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText());
}
 
Example 7
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Opens a new window.
 *
 * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
 *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
 * @param name the name
 * @param features the features
 * @param replace whether to replace in the history list or no
 * @return the newly opened window, or {@code null} if popup windows have been disabled
 * @see com.gargoylesoftware.htmlunit.WebClientOptions#isPopupBlockerEnabled()
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536651.aspx">MSDN documentation</a>
 */
@JsxFunction
public WindowProxy open(final Object url, final Object name, final Object features,
        final Object replace) {
    String urlString = null;
    if (url != Undefined.instance) {
        urlString = Context.toString(url);
    }
    String windowName = "";
    if (name != Undefined.instance) {
        windowName = Context.toString(name);
    }
    String featuresString = null;
    if (features != Undefined.instance) {
        featuresString = Context.toString(features);
    }
    final WebClient webClient = getWebWindow().getWebClient();

    if (webClient.getOptions().isPopupBlockerEnabled()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Ignoring window.open() invocation because popups are blocked.");
        }
        return null;
    }

    boolean replaceCurrentEntryInBrowsingHistory = false;
    if (replace != Undefined.instance) {
        replaceCurrentEntryInBrowsingHistory = Context.toBoolean(replace);
    }
    if ((featuresString != null || replaceCurrentEntryInBrowsingHistory) && LOG.isDebugEnabled()) {
        LOG.debug(
               "window.open: features and replaceCurrentEntryInBrowsingHistory "
                + "not implemented: url=[" + urlString
                + "] windowName=[" + windowName
                + "] features=[" + featuresString
                + "] replaceCurrentEntry=[" + replaceCurrentEntryInBrowsingHistory
                + "]");
    }

    // if specified name is the name of an existing window, then hold it
    if (StringUtils.isEmpty(urlString) && !"".equals(windowName)) {
        try {
            final WebWindow webWindow = webClient.getWebWindowByName(windowName);
            return getProxy(webWindow);
        }
        catch (final WebWindowNotFoundException e) {
            // nothing
        }
    }
    final URL newUrl = makeUrlForOpenWindow(urlString);
    final WebWindow newWebWindow = webClient.openWindow(newUrl, windowName, webWindow_);
    return getProxy(newWebWindow);
}