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.html;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.CollectingAlertHandler;
21  import org.htmlunit.MockWebConnection;
22  import org.htmlunit.SimpleWebTestCase;
23  import org.htmlunit.WebClient;
24  import org.htmlunit.html.HtmlAnchor;
25  import org.htmlunit.html.HtmlButton;
26  import org.htmlunit.html.HtmlElement;
27  import org.htmlunit.html.HtmlPage;
28  import org.htmlunit.junit.annotation.Alerts;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for {@link HTMLElement}.
33   *
34   * @author Brad Clarke
35   * @author Chris Erskine
36   * @author David D. Kilzer
37   * @author Daniel Gredler
38   * @author Marc Guillemot
39   * @author Hans Donner
40   * @author <a href="mailto:george@murnock.com">George Murnock</a>
41   * @author Bruce Faulkner
42   * @author Ahmed Ashour
43   * @author Sudhan Moghe
44   * @author Ethan Glasser-Camp
45   * @author Frank Danek
46   */
47  public class HTMLElement3Test extends SimpleWebTestCase {
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      public void clickHashAnchor() throws Exception {
54          final String html = DOCTYPE_HTML
55              + "<html><head><title>HashAnchor</title></head>\n"
56              + "<body>\n"
57              + "  <script language='javascript'>\n"
58              + "    function test() {alert('test hash');}\n"
59              + "  </script>\n"
60              + "  <a onClick='javascript:test();' href='#' name='hash'>Click</a>\n"
61              + "</body>\n"
62              + "</html>";
63          final String[] expectedAlerts = {"test hash"};
64          // first use direct load
65          final List<String> loadCollectedAlerts = new ArrayList<>();
66          final HtmlPage loadPage = loadPage(html, loadCollectedAlerts);
67          final HtmlAnchor loadHashAnchor = loadPage.getAnchorByName("hash");
68          loadHashAnchor.click();
69  
70          assertEquals(expectedAlerts, loadCollectedAlerts);
71  
72          // finally try via the client
73          final WebClient webClient = getWebClient();
74          final MockWebConnection webConnection = new MockWebConnection();
75          webConnection.setResponse(URL_FIRST, html);
76          webClient.setWebConnection(webConnection);
77          final CollectingAlertHandler clientCollectedAlertsHandler = new CollectingAlertHandler();
78          webClient.setAlertHandler(clientCollectedAlertsHandler);
79          final HtmlPage clientPage = webClient.getPage(URL_FIRST);
80          final HtmlAnchor clientHashAnchor = clientPage.getAnchorByName("hash");
81          clientHashAnchor.click();
82  
83          assertEquals(expectedAlerts, clientCollectedAlertsHandler.getCollectedAlerts());
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts({"onfocus text1", "onfocus text2", "onfocus text1", "onfocus text2"})
91      public void onFocusOnWindowFocusGain() throws Exception {
92          final WebClient webClient = getWebClient();
93          final MockWebConnection webConnection = new MockWebConnection();
94          final List<String> collectedAlerts = new ArrayList<>();
95  
96          webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
97  
98          final String firstHtml = DOCTYPE_HTML
99              + "<html><head><title>First</title></head>\n"
100             + "<body><form name='form1'>\n"
101             + "<input id='text1' onfocus='alert(\"onfocus text1\")'>\n"
102             + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
103             + "</form></body></html>";
104         webConnection.setResponse(URL_FIRST, firstHtml);
105 
106         final String secondHtml = DOCTYPE_HTML
107             + "<html><head><title>Second</title></head>\n"
108             + "<body onLoad='doTest()'>\n"
109             + "<input id='text2' onfocus='alert(\"onfocus text2\")'>\n"
110             + "<script>\n"
111             + "  function doTest() {\n"
112             + "    opener.document.getElementById('text1').focus();\n"
113             + "    document.getElementById('text2').focus();\n"
114             + "  }\n"
115             + "</script></body></html>";
116 
117         webConnection.setResponse(URL_SECOND, secondHtml);
118         webClient.setWebConnection(webConnection);
119 
120         final HtmlPage firstPage = webClient.getPage(URL_FIRST);
121         assertEquals("First", firstPage.getTitleText());
122 
123         final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
124         final HtmlPage secondPage = buttonA.click();
125         assertEquals("Second", secondPage.getTitleText());
126 
127         webClient.setCurrentWindow(firstPage.getEnclosingWindow());
128         webClient.setCurrentWindow(secondPage.getEnclosingWindow());
129         assertEquals(getExpectedAlerts(), collectedAlerts);
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts({"onblur text2", "onblur text1"})
137     public void onBlurOnWindowFocusChange() throws Exception {
138         final WebClient webClient = getWebClient();
139         final MockWebConnection webConnection = new MockWebConnection();
140         final List<String> collectedAlerts = new ArrayList<>();
141 
142         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
143 
144         final String firstHtml = DOCTYPE_HTML
145             + "<html><head><title>First</title></head>\n"
146             + "<body><form name='form1'>\n"
147             + "<input id='text1' onblur='alert(\"onblur text1\")'>\n"
148             + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
149             + "</form></body></html>";
150         webConnection.setResponse(URL_FIRST, firstHtml);
151 
152         final String secondHtml = DOCTYPE_HTML
153             + "<html><head><title>Second</title></head>\n"
154             + "<body onLoad='doTest()'>\n"
155             + "<input id='text2' onblur='alert(\"onblur text2\")'>\n"
156             + "<script>\n"
157             + "  function doTest() {\n"
158             + "    opener.document.getElementById('text1').focus();\n"
159             + "    document.getElementById('text2').focus();\n"
160             + "  }\n"
161             + "</script></body></html>";
162 
163         webConnection.setResponse(URL_SECOND, secondHtml);
164         webClient.setWebConnection(webConnection);
165 
166         final HtmlPage firstPage = webClient.getPage(URL_FIRST);
167         assertEquals("First", firstPage.getTitleText());
168 
169         final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
170         final HtmlPage secondPage = buttonA.click();
171         assertEquals("Second", secondPage.getTitleText());
172         webClient.setCurrentWindow(firstPage.getEnclosingWindow());
173         webClient.setCurrentWindow(secondPage.getEnclosingWindow());
174         assertEquals(getExpectedAlerts(), collectedAlerts);
175     }
176 
177     /**
178      * @throws Exception if the test fails
179      */
180     @Test
181     public void offsetHeight() throws Exception {
182         final String html = DOCTYPE_HTML
183             + "<html><head></head>\n"
184             + "<body>\n"
185             + "<div>1</div>\n"
186             + "</body>\n"
187             + "</html>";
188         final HtmlPage page = loadPage(html);
189         final HTMLElement host = (HTMLElement) page.<HtmlElement>getFirstByXPath("//div").getScriptableObject();
190         final int offsetHeight = host.getOffsetHeight();
191         assertTrue(offsetHeight > 0);
192     }
193 
194 }