1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class HTMLElement3Test extends SimpleWebTestCase {
48
49
50
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
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
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
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
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
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 }