View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link Popup}.
30   *
31   * @author Marc Guillemot
32   * @author Ronald Brill
33   */
34  public class PopupTest extends SimpleWebTestCase {
35  
36      /**
37       * Test that the opened window becomes the current one.
38       * @throws Exception if the test fails
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  }