Search in sources :

Example 1 with WebWindowNotFoundException

use of com.gargoylesoftware.htmlunit.WebWindowNotFoundException in project htmlunit by HtmlUnit.

the class WindowTest method openWindow_top.

/**
 * _top is a magic name. If we call open(url, '_top') then the top level
 * window must be reloaded.
 * @throws Exception if the test fails
 */
@Test
public void openWindow_top() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "  <iframe name='secondFrame' id='secondFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "  <iframe name='thirdFrame' id='thirdFrame' src='" + URL_THIRD + "'></iframe>\n" + "</body></html>";
    final String thirdContent = "<html><head><title>Third</title></head><body>\n" + "    <a id='link' onClick='window.open(\"http://fourth\", \"_top\"); " + "return false;'>Click me</a>\n" + "</body></html>";
    final String fourthContent = "<html><head><title>Fourth</title></head><body></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webConnection.setResponse(URL_THIRD, thirdContent);
    webConnection.setResponse(new URL("http://fourth/"), fourthContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    assertEquals("First", firstPage.getTitleText());
    final HtmlInlineFrame secondFrame = firstPage.getHtmlElementById("secondFrame");
    final HtmlPage secondPage = (HtmlPage) secondFrame.getEnclosedPage();
    assertEquals("Second", secondPage.getTitleText());
    final HtmlInlineFrame thirdFrame = secondPage.getHtmlElementById("thirdFrame");
    final HtmlPage thirdPage = (HtmlPage) thirdFrame.getEnclosedPage();
    assertEquals("Third", thirdPage.getTitleText());
    assertSame(webClient.getCurrentWindow(), firstWebWindow);
    assertNotSame(firstWebWindow, secondPage);
    final HtmlAnchor anchor = thirdPage.getHtmlElementById("link");
    final HtmlPage fourthPage = anchor.click();
    final WebWindow fourthWebWindow = fourthPage.getEnclosingWindow();
    assertSame(firstWebWindow, fourthWebWindow);
    assertSame(fourthWebWindow, fourthWebWindow.getTopWindow());
    try {
        webClient.getWebWindowByName("secondFrame");
        fail("Did not expect secondFrame to still exist after click.");
    } catch (final WebWindowNotFoundException exception) {
    // Expected path
    }
    try {
        webClient.getWebWindowByName("thirdFrame");
        fail("Did not expect thirdFrame to still exist after click.");
    } catch (final WebWindowNotFoundException exception) {
    // Expected path
    }
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame) WebWindowNotFoundException(com.gargoylesoftware.htmlunit.WebWindowNotFoundException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 2 with WebWindowNotFoundException

use of com.gargoylesoftware.htmlunit.WebWindowNotFoundException in project htmlunit by HtmlUnit.

the class WindowTest method openWindow_parent.

/**
 * {@code _parent} is a magic name. If we call open(url, '_parent') then the parent window must be reloaded.
 *
 * @throws Exception if the test fails
 */
@Test
public void openWindow_parent() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "  <iframe name='secondFrame' id='secondFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "  <iframe name='thirdFrame' id='thirdFrame' src='" + URL_THIRD + "'></iframe>\n" + "</body></html>";
    final String thirdContent = "<html><head><title>Third</title></head><body>\n" + "    <a id='link' onClick='window.open(\"http://fourth\", \"_parent\"); " + "return false;'>Click me</a>\n" + "</body></html>";
    final String fourthContent = "<html><head><title>Fourth</title></head><body></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webConnection.setResponse(URL_THIRD, thirdContent);
    webConnection.setResponse(new URL("http://fourth/"), fourthContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    assertEquals("First", firstPage.getTitleText());
    final HtmlInlineFrame secondFrame = firstPage.getHtmlElementById("secondFrame");
    final HtmlPage secondPage = (HtmlPage) secondFrame.getEnclosedPage();
    assertEquals("Second", secondPage.getTitleText());
    final HtmlInlineFrame thirdFrame = secondPage.getHtmlElementById("thirdFrame");
    final HtmlPage thirdPage = (HtmlPage) thirdFrame.getEnclosedPage();
    assertEquals("Third", thirdPage.getTitleText());
    assertSame(webClient.getCurrentWindow(), firstWebWindow);
    assertNotSame(firstWebWindow, secondFrame);
    final HtmlAnchor anchor = thirdPage.getHtmlElementById("link");
    final HtmlPage fourthPage = anchor.click();
    final WebWindow fourthWebWindow = fourthPage.getEnclosingWindow();
    assertSame(secondFrame.getEnclosedWindow(), fourthWebWindow);
    try {
        final WebWindow namedWindow = webClient.getWebWindowByName("secondFrame");
        assertSame(namedWindow.getEnclosedPage(), fourthPage);
    // Expected path
    } catch (final WebWindowNotFoundException exception) {
        fail("Expected secondFrame would be found after click.");
    }
    try {
        webClient.getWebWindowByName("thirdFrame");
        fail("Did not expect thirdFrame to still exist after click.");
    } catch (final WebWindowNotFoundException exception) {
    // Expected path
    }
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame) WebWindowNotFoundException(com.gargoylesoftware.htmlunit.WebWindowNotFoundException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 3 with WebWindowNotFoundException

use of com.gargoylesoftware.htmlunit.WebWindowNotFoundException in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method open.

/**
 * 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);
}
Also used : WebClient(com.gargoylesoftware.htmlunit.WebClient) WebWindowNotFoundException(com.gargoylesoftware.htmlunit.WebWindowNotFoundException) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with WebWindowNotFoundException

use of com.gargoylesoftware.htmlunit.WebWindowNotFoundException in project htmlunit by HtmlUnit.

the class WindowTest method openWindow_blank.

/**
 * _blank is a magic name. If we call open(url, '_blank') then a new
 * window must be loaded.
 * @throws Exception if the test fails
 */
@Test
public void openWindow_blank() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "  <iframe name='secondFrame' id='secondFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "  <a id='link' " + "onClick='window.open(\"" + URL_THIRD + "\", \"_blank\").focus(); '>\n" + "Click me</a>\n" + "</body></html>";
    final String thirdContent = "<html><head><title>Third</title></head><body>\n" + "</body></html>";
    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 firstWindow = firstPage.getEnclosingWindow();
    final HtmlInlineFrame secondFrame = firstPage.getHtmlElementById("secondFrame");
    final HtmlPage secondPage = (HtmlPage) secondFrame.getEnclosedPage();
    assertEquals("Second", secondPage.getTitleText());
    try {
        assertEquals(secondFrame.getEnclosedWindow(), webClient.getWebWindowByName("secondFrame"));
    // Expected path
    } catch (final WebWindowNotFoundException exception) {
        fail("Expected secondFrame would be found before click.");
    }
    final HtmlAnchor anchor = secondPage.getHtmlElementById("link");
    final HtmlPage thirdPage = anchor.click();
    assertEquals("Third", thirdPage.getTitleText());
    final WebWindow thirdWindow = thirdPage.getEnclosingWindow();
    assertNotSame(firstWindow, thirdWindow);
    assertEquals("", thirdWindow.getName());
    assertEquals(thirdWindow, thirdWindow.getTopWindow());
    try {
        assertEquals(secondFrame.getEnclosedWindow(), webClient.getWebWindowByName("secondFrame"));
    // Expected path
    } catch (final WebWindowNotFoundException exception) {
        fail("Expected secondFrame would be found after click.");
    }
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame) WebWindowNotFoundException(com.gargoylesoftware.htmlunit.WebWindowNotFoundException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Aggregations

WebClient (com.gargoylesoftware.htmlunit.WebClient)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)4 WebWindowNotFoundException (com.gargoylesoftware.htmlunit.WebWindowNotFoundException)4 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)3 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)3 HtmlInlineFrame (com.gargoylesoftware.htmlunit.html.HtmlInlineFrame)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 URL (java.net.URL)3 Test (org.junit.Test)3 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 ArrayList (java.util.ArrayList)1