Search in sources :

Example 6 with WebWindowEvent

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

the class WindowTest method openWindow_text.

/**
 * 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_text() 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 = "Hello World";
    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_PLAIN, 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_PLAIN, 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 7 with WebWindowEvent

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

the class WindowTest method openWindow_image.

/**
 * Open a window with only an image for content, then try to set focus to it.
 *
 * @throws Exception if the test fails
 */
@Test
public void openWindow_image() 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 = new String(new char[] { 'G', 'I', 'F', '8', '9', 'a', 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xfe, 0xd4, 0xaf, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b });
    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.IMAGE_GIF, 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.IMAGE_GIF, 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 8 with WebWindowEvent

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

the class WindowTest method openWindow.

/**
 * @throws Exception if the test fails
 */
@Test
public void openWindow() 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" + "<form name='form1'>\n" + "  <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"MyNewWindow\").focus(); " + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<script>alert(self.name)</script>\n" + "</body></html>";
    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);
        }
    });
    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 HtmlAnchor anchor = firstPage.getHtmlElementById("link");
    final HtmlPage secondPage = anchor.click();
    assertNotSame(firstPage, secondPage);
    // Expecting contentChanged, opened, contentChanged
    assertEquals(3, events.size());
    final WebWindow firstWebWindow = (WebWindow) events.get(0).getSource();
    final WebWindow secondWebWindow = (WebWindow) events.get(2).getSource();
    assertSame(webClient.getCurrentWindow(), secondWebWindow);
    assertEquals("MyNewWindow", secondWebWindow.getName());
    assertEquals("First", ((HtmlPage) firstWebWindow.getEnclosedPage()).getTitleText());
    assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
    final WebWindowEvent changedEvent = events.get(2);
    assertNull(changedEvent.getOldPage());
    assertEquals("Second", ((HtmlPage) changedEvent.getNewPage()).getTitleText());
    assertEquals(new String[] { "MyNewWindow" }, 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) 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