1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.swing.Popup;
21
22 import org.htmlunit.SimpleWebTestCase;
23 import org.htmlunit.html.HtmlElement;
24 import org.htmlunit.html.HtmlPage;
25 import org.htmlunit.junit.BrowserRunner;
26 import org.htmlunit.junit.annotation.Alerts;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29
30
31
32
33
34
35 @RunWith(BrowserRunner.class)
36 public class PopupTest extends SimpleWebTestCase {
37
38
39
40
41
42 @Test
43 @Alerts("Pop-up window is Open")
44 public void popupWindowBecomesCurrent() throws Exception {
45 final String content = DOCTYPE_HTML
46 + "<html><head><title>First</title><body>\n"
47 + "<span id='button' onClick='openPopup()'>Push me</span>\n"
48 + "<script>\n"
49 + " function openPopup() {\n "
50 + " window.open('', '_blank', 'width=640, height=600, scrollbars=yes');\n"
51 + " alert('Pop-up window is Open');\n "
52 + " }\n"
53 + "</script>\n"
54 + "</body></html>";
55
56 final List<String> collectedAlerts = new ArrayList<>();
57 final HtmlPage page = loadPage(content, collectedAlerts);
58 final HtmlElement button = page.getHtmlElementById("button");
59
60 final HtmlPage secondPage = button.click();
61 final String[] expectedAlerts = {"Pop-up window is Open"};
62 assertEquals(expectedAlerts, collectedAlerts);
63 assertEquals("about:blank", secondPage.getUrl());
64 assertSame(secondPage.getEnclosingWindow(), secondPage.getWebClient().getCurrentWindow());
65 }
66 }