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.annotation.Alerts;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32
33
34 public class PopupTest extends SimpleWebTestCase {
35
36
37
38
39
40 @Test
41 @Alerts("Pop-up window is Open")
42 public void popupWindowBecomesCurrent() throws Exception {
43 final String content = DOCTYPE_HTML
44 + "<html><head><title>First</title><body>\n"
45 + "<span id='button' onClick='openPopup()'>Push me</span>\n"
46 + "<script>\n"
47 + " function openPopup() {\n "
48 + " window.open('', '_blank', 'width=640, height=600, scrollbars=yes');\n"
49 + " alert('Pop-up window is Open');\n "
50 + " }\n"
51 + "</script>\n"
52 + "</body></html>";
53
54 final List<String> collectedAlerts = new ArrayList<>();
55 final HtmlPage page = loadPage(content, collectedAlerts);
56 final HtmlElement button = page.getHtmlElementById("button");
57
58 final HtmlPage secondPage = button.click();
59 final String[] expectedAlerts = {"Pop-up window is Open"};
60 assertEquals(expectedAlerts, collectedAlerts);
61 assertEquals("about:blank", secondPage.getUrl());
62 assertSame(secondPage.getEnclosingWindow(), secondPage.getWebClient().getCurrentWindow());
63 }
64 }