1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20 import org.openqa.selenium.By;
21 import org.openqa.selenium.WebDriver;
22 import org.openqa.selenium.WebElement;
23
24
25
26
27
28
29
30
31 public class EventNodeTest extends WebDriverTestCase {
32
33
34
35
36 @Test
37 @Alerts("fireEvent not available")
38 public void fireEvent() throws Exception {
39 final String html = DOCTYPE_HTML
40 + "<html><head>\n"
41 + "<script>\n"
42 + LOG_TITLE_FUNCTION
43 + " function test() {\n"
44 + " var form = document.getElementById('myForm');\n"
45 + " if (!form.fireEvent) { log('fireEvent not available'); return }\n"
46 + " log(form.fireEvent('onsubmit'));\n"
47 + " }\n"
48 + "</script>\n"
49 + "</head><body onload='test()'>\n"
50 + " <form id='myForm'>\n"
51 + " </form>\n"
52 + "</body></html>";
53
54 loadPageVerifyTitle2(html);
55 }
56
57
58
59
60 @Test
61 @Alerts("createEventObject not available")
62 public void fireEvent_initFromTemplate() throws Exception {
63 final String html = DOCTYPE_HTML
64 + "<html>\n"
65 + "<head>\n"
66 + " <script>\n"
67 + LOG_TITLE_FUNCTION
68 + " function test() {\n"
69 + " if (!document.createEventObject) { log('createEventObject not available'); return }\n"
70 + " var myEvent = document.createEventObject();\n"
71 + " myEvent.eventType = 'onclick';\n"
72 + " myEvent.foo = 'hello';\n"
73 + " var butt = document.getElementById('theButton');\n"
74 + " butt.fireEvent('onclick', myEvent);\n"
75 + " }\n"
76 + " </script>\n"
77 + "</head><body onload='test()'>\n"
78 + " <span id='theButton' onclick='log(event.foo)'>a span</span>\n"
79 + "</body></html>";
80
81 loadPageVerifyTitle2(html);
82 }
83
84
85
86
87 @Test
88 @Alerts({"mousedown span", "mouseup span", "click span",
89 "mousedown text", "focus text", "mouseup text", "click text",
90 "mousedown image", "focus image", "mouseup image", "click image",
91 "mousedown textarea", "focus textarea", "mouseup textarea", "click textarea"})
92 public void clickEvents() throws Exception {
93 final String html = DOCTYPE_HTML
94 + "<html>\n"
95 + "<head>\n"
96 + " <script>\n"
97 + LOG_TEXTAREA_FUNCTION
98 + " </script>\n"
99 + "</head><body>\n"
100 + " <span id='testSpan' onfocus=\"log('will not be triggered')\" onmousedown=\"log('mousedown span')\""
101 + " onclick=\"log('click span')\" onmouseup=\"log('mouseup span')\">test span</span>\n"
102 + " <form>\n"
103 + " <input type='text' id='testInput' onmousedown=\"log('mousedown text')\""
104 + " onclick=\"log('click text')\" onmouseup=\"log('mouseup text')\" onfocus=\"log('focus text')\">\n"
105 + " <input type='image' id='testImage' onmousedown=\"log('mousedown image')\""
106 + " onclick=\"log('click image'); return false;\" onmouseup=\"log('mouseup image')\""
107 + " onfocus=\"log('focus image')\">\n"
108 + " <textarea id='testTextarea' onfocus=\"log('focus textarea')\""
109 + " onmousedown=\"log('mousedown textarea')\" onclick=\"log('click textarea')\""
110 + " onmouseup=\"log('mouseup textarea')\" onfocus=\"log('focus textarea')\"></textarea>\n"
111 + " </form>\n"
112 + LOG_TEXTAREA
113 + "</body></html>";
114
115 final WebDriver driver = loadPage2(html);
116 driver.findElement(By.id("testSpan")).click();
117 driver.findElement(By.id("testInput")).click();
118 driver.findElement(By.id("testImage")).click();
119 driver.findElement(By.id("testTextarea")).click();
120
121 verifyTextArea2(driver, getExpectedAlerts());
122 }
123
124
125
126
127 @Test
128 @Alerts({"mousedown label", "mouseup label", "click label", "focus text", "click text"})
129 public void clickEventsLabel() throws Exception {
130 final String html = DOCTYPE_HTML
131 + "<html>\n"
132 + "<head>\n"
133 + " <script>\n"
134 + LOG_TEXTAREA_FUNCTION
135 + " </script>\n"
136 + "</head><body>\n"
137 + " <label id='testLabel' for='testInput'"
138 + " onfocus=\"log('will not be triggered')\" onmousedown=\"log('mousedown label')\""
139 + " onclick=\"log('click label')\" onmouseup=\"log('mouseup label')\">test label</label>\n"
140 + " <form>\n"
141 + " <input type='text' id='testInput' onmousedown=\"log('mousedown text')\""
142 + " onclick=\"log('click text')\" onmouseup=\"log('mouseup text')\" onfocus=\"log('focus text')\">\n"
143 + " </form>\n"
144 + LOG_TEXTAREA
145 + "</body></html>";
146
147 final WebDriver driver = loadPage2(html);
148 driver.findElement(By.id("testLabel")).click();
149
150 verifyTextArea2(driver, getExpectedAlerts());
151 }
152
153
154
155
156
157 @Test
158 @Alerts({"focus", "keydown", "keypress", "keyup", "change", "blur"})
159 public void eventOrder() throws Exception {
160 final String html = DOCTYPE_HTML
161 + "<html>\n"
162 + "<head>\n"
163 + " <script>\n"
164 + LOG_TEXTAREA_FUNCTION
165 + " </script>\n"
166 + "</head><body>\n"
167 + "<form>\n"
168 + " <input name='foo' id='foo' onfocus=\"log('focus')\" onblur=\"log('blur')\" onchange=\"log('change')\""
169 + " onkeydown=\"log('keydown')\" onkeypress=\"log('keypress')\" onkeyup=\"log('keyup')\">\n"
170 + " <input name='other' id='other'>\n"
171 + "</form>\n"
172 + LOG_TEXTAREA
173 + "</body></html>";
174
175 final WebDriver webDriver = loadPage2(html);
176 final WebElement textField = webDriver.findElement(By.id("foo"));
177 textField.click();
178 textField.sendKeys("a");
179 webDriver.findElement(By.id("other")).click();
180
181 verifyTextArea2(webDriver, getExpectedAlerts());
182 }
183 }