Search in sources :

Example 1 with WebWindowEvent

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

the class WindowTest method openWindow_self.

/**
 * _self is a magic name. If we call open(url, '_self') then the current window must be
 * reloaded.
 * @throws Exception if the test fails
 */
@Test
public void openWindow_self() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + "  <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_self\"); " + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final HtmlPage secondPage = anchor.click();
    assertEquals("First", firstPage.getTitleText());
    assertEquals("Second", secondPage.getTitleText());
    assertEquals(1, events.size());
    final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
    assertSame(webClient.getCurrentWindow(), firstWebWindow);
    assertSame(firstWebWindow, secondWebWindow);
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 2 with WebWindowEvent

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

the class WindowTest method openWindow_xml.

/**
 * Open a window with only XML for content, then try to set focus to it.
 *
 * @throws Exception if the test fails
 */
@Test
public void openWindow_xml() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + "  <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<junk></junk>\n";
    final List<NameValuePair> emptyList = Collections.emptyList();
    webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", MimeType.TEXT_HTML, emptyList);
    webConnection.setResponse(URL_SECOND, secondContent, 200, "OK", MimeType.TEXT_XML, emptyList);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final Page secondPage = anchor.click();
    assertEquals("First", firstPage.getTitleText());
    assertEquals(MimeType.TEXT_XML, secondPage.getWebResponse().getContentType());
    assertEquals(2, events.size());
    final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
    assertSame(webClient.getCurrentWindow(), secondWebWindow);
    assertNotSame(firstWebWindow, secondWebWindow);
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) Test(org.junit.Test)

Example 3 with WebWindowEvent

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

the class WindowTest method openWindow_html.

/**
 * Open a window with only text for content, then try to set focus to it.
 *
 * @throws Exception if the test fails
 */
@Test
public void openWindow_html() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + "  <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<p>Hello World</p>\n" + "</body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final Page secondPage = anchor.click();
    assertEquals("First", firstPage.getTitleText());
    assertEquals(MimeType.TEXT_HTML, secondPage.getWebResponse().getContentType());
    assertEquals(2, events.size());
    final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
    assertSame(webClient.getCurrentWindow(), secondWebWindow);
    assertNotSame(firstWebWindow, secondWebWindow);
}
Also used : HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 4 with WebWindowEvent

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

the class WindowTest method close.

/**
 * Test closing using JavaScript.
 * @throws Exception if the test fails
 */
@Test
public void close() 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" + "<a href='" + URL_SECOND + "' id='link' target='_blank'>Link</a>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<h1>Second</h1><form>\n" + "<input type='submit' name='action' value='Close' id='button' " + "onclick='window.close(); return false;'>\n" + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    assertEquals(1, webClient.getWebWindows().size());
    final WebWindow firstWindow = firstPage.getEnclosingWindow();
    final HtmlPage secondPage = firstPage.getHtmlElementById("link").click();
    assertEquals("Second", secondPage.getTitleText());
    assertEquals(2, webClient.getWebWindows().size());
    final WebWindow secondWindow = secondPage.getEnclosingWindow();
    assertNotSame(firstWindow, secondWindow);
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    secondPage.getHtmlElementById("button").click();
    final List<WebWindowEvent> expectedEvents = Arrays.asList(new WebWindowEvent[] { new WebWindowEvent(secondWindow, WebWindowEvent.CLOSE, secondPage, null) });
    assertEquals(expectedEvents, events);
    assertEquals(1, webClient.getWebWindows().size());
    assertEquals(firstWindow, webClient.getCurrentWindow());
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Also used : 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) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) Test(org.junit.Test)

Example 5 with WebWindowEvent

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

the class WindowTest method openWindow_javascript.

/**
 * Open a window with only JavaScript for content, then try to set focus to it.
 *
 * @throws Exception if the test fails
 */
@Test
public void openWindow_javascript() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + "  <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "var x=1;\n";
    final List<NameValuePair> emptyList = Collections.emptyList();
    webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", MimeType.TEXT_HTML, emptyList);
    webConnection.setResponse(URL_SECOND, secondContent, 200, "OK", "text/javascript", emptyList);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final List<WebWindowEvent> events = new LinkedList<>();
    webClient.addWebWindowListener(new WebWindowListener() {

        @Override
        public void webWindowOpened(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowContentChanged(final WebWindowEvent event) {
            events.add(event);
        }

        @Override
        public void webWindowClosed(final WebWindowEvent event) {
            events.add(event);
        }
    });
    final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
    final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final Page secondPage = anchor.click();
    assertEquals("First", firstPage.getTitleText());
    assertEquals("text/javascript", secondPage.getWebResponse().getContentType());
    assertEquals(2, events.size());
    final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
    assertSame(webClient.getCurrentWindow(), secondWebWindow);
    assertNotSame(firstWebWindow, secondWebWindow);
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) WebWindowListener(com.gargoylesoftware.htmlunit.WebWindowListener) WebWindowEvent(com.gargoylesoftware.htmlunit.WebWindowEvent) Test(org.junit.Test)

Aggregations

MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)8 WebClient (com.gargoylesoftware.htmlunit.WebClient)8 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)8 WebWindowEvent (com.gargoylesoftware.htmlunit.WebWindowEvent)8 WebWindowListener (com.gargoylesoftware.htmlunit.WebWindowListener)8 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)7 Page (com.gargoylesoftware.htmlunit.Page)5 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)4 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)2 ArrayList (java.util.ArrayList)2