1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.Test;
21 import org.openqa.selenium.By;
22 import org.openqa.selenium.ElementNotInteractableException;
23 import org.openqa.selenium.InvalidElementStateException;
24 import org.openqa.selenium.Keys;
25 import org.openqa.selenium.WebDriver;
26 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
27
28
29
30
31
32
33
34 public final class DomElementTest extends WebDriverTestCase {
35
36
37
38
39 @Test
40 @Alerts({"2", "2"})
41 public void getElementsByTagName() throws Exception {
42 final String html = DOCTYPE_HTML
43 + "<html><head>\n"
44 + "<script>\n"
45 + LOG_TITLE_FUNCTION
46 + "function test() {\n"
47 + " log(document.f1.getElementsByTagName('input').length);\n"
48 + " log(document.f1.getElementsByTagName('INPUT').length);\n"
49 + "}\n"
50 + "</script></head>\n"
51 + "<body onload='test()'>\n"
52 + " <form name='f1'>\n"
53 + " <input>\n"
54 + " <INPUT>\n"
55 + " </form>\n"
56 + "</body></html>";
57
58 final WebDriver driver = loadPageVerifyTitle2(html);
59 if (driver instanceof HtmlUnitDriver) {
60 final HtmlPage page = (HtmlPage) getEnclosedPage();
61 assertEquals(2, page.getForms().get(0).getElementsByTagName("input").size());
62 assertEquals(2, page.getForms().get(0).getElementsByTagName("INPUT").size());
63 }
64 }
65
66
67
68
69 @Test
70 public void clickInvisible() throws Exception {
71 final String html = DOCTYPE_HTML
72 + "<html>\n"
73 + "<body>\n"
74 + " <a id='link' style='display: none'>Click me</a>\n"
75 + "</body></html>";
76
77 final WebDriver driver = loadPage2(html);
78 Assertions.assertThrows(ElementNotInteractableException.class,
79 () -> driver.findElement(By.id("link")).click());
80 }
81
82
83
84
85 @Test
86 @Alerts({"false", "true"})
87 public void clickFromJavaScript() throws Exception {
88 final String html = "<head>\n"
89 + "<script>\n"
90 + LOG_TITLE_FUNCTION
91 + " function test() {\n"
92 + " try {\n"
93 + " var e = document.getElementById('id1');\n"
94 + " log(e.checked);\n"
95 + " e.click();\n"
96 + " log(e.checked);\n"
97 + " } catch(e) {log(e)}\n"
98 + " }\n"
99 + "</script>\n"
100 + "</head>\n"
101 + "<body onload='test()'>\n"
102 + " <div style='display: none;'>\n"
103 + " <input type='checkbox' id='id1'>\n"
104 + " </div>\n"
105 + "</body>\n";
106
107 loadPageVerifyTitle2(html);
108 }
109
110
111
112
113 @Test
114 public void clickDisabled() throws Exception {
115 final String html = DOCTYPE_HTML
116 + "<html>\n"
117 + "<body>\n"
118 + " <button id='id1' disabled>Click Me</button>\n"
119 + "</body></html>";
120
121 final WebDriver driver = loadPage2(html);
122 driver.findElement(By.id("id1")).click();
123 }
124
125
126
127
128 @Test
129 public void sendKeysToDisabled() throws Exception {
130 final String html = DOCTYPE_HTML
131 + "<html>\n"
132 + "<body>\n"
133 + " <input id='id1' disabled>\n"
134 + "</body></html>";
135
136 final WebDriver driver = loadPage2(html);
137 Assertions.assertThrows(InvalidElementStateException.class,
138 () -> driver.findElement(By.id("id1")).sendKeys("Hello"));
139 }
140
141
142
143
144 @Test
145 public void sendEnterKeyWithHiddenSubmit() throws Exception {
146 final String html = DOCTYPE_HTML
147 + "<html><head></head>\n"
148 + "<body>\n"
149 + " <form id='myForm' action='" + URL_SECOND + "'>\n"
150 + " <input id='myText' type='text'>\n"
151 + " <input id='myButton' type='submit' style='display: none;'>Submit</input>\n"
152 + " </form>\n"
153 + "</body></html>";
154 final String secondContent = "second content";
155
156 getMockWebConnection().setResponse(URL_SECOND, secondContent);
157
158 final WebDriver driver = loadPage2(html);
159 driver.findElement(By.id("myText")).sendKeys(Keys.ENTER);
160 if (useRealBrowser()) {
161 Thread.sleep(400);
162 }
163
164 assertEquals(2, getMockWebConnection().getRequestCount());
165 assertEquals(URL_SECOND, getMockWebConnection().getLastWebRequest().getUrl());
166 }
167
168
169
170
171 @Test
172 public void sendEnterKey() throws Exception {
173 final String html = DOCTYPE_HTML
174 + "<html><head></head>\n"
175 + "<body>\n"
176 + " <form id='myForm' action='" + URL_SECOND + "'>\n"
177 + " <input id='myText' type='text'>\n"
178 + " </form>\n"
179 + "</body></html>";
180 final String secondContent = "second content";
181
182 getMockWebConnection().setResponse(URL_SECOND, secondContent);
183
184 final WebDriver driver = loadPage2(html);
185 driver.findElement(By.id("myText")).sendKeys(Keys.ENTER);
186 if (useRealBrowser()) {
187 Thread.sleep(400);
188 }
189
190 assertEquals(2, getMockWebConnection().getRequestCount());
191 assertEquals(URL_SECOND, getMockWebConnection().getLastWebRequest().getUrl());
192 }
193
194
195
196
197 @Test
198 public void clickHiddenSubmit() throws Exception {
199 final String html = DOCTYPE_HTML
200 + "<html><head></head>\n"
201 + "<body>\n"
202 + " <form id='myForm' action='" + URL_SECOND + "'>\n"
203 + " <input id='myButton' type='submit' style='display: none;'>Submit</input>\n"
204 + " </form>\n"
205 + "</body></html>";
206 final String secondContent = "second content";
207
208 getMockWebConnection().setResponse(URL_SECOND, secondContent);
209
210 final WebDriver driver = loadPage2(html);
211 Assertions.assertThrows(ElementNotInteractableException.class,
212 () -> driver.findElement(By.id("myButton")).click());
213 }
214
215 }