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.SimpleWebTestCase;
21 import org.htmlunit.html.HtmlPage;
22 import org.htmlunit.html.HtmlTextInput;
23 import org.htmlunit.junit.annotation.Alerts;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29
30
31
32
33
34 public class HTMLInputElement2Test extends SimpleWebTestCase {
35
36
37
38
39 @Test
40 @Alerts({"hello", "me te"})
41 public void selectionRange() throws Exception {
42 final String html = DOCTYPE_HTML
43 + "<html><head><title>foo</title><script>\n"
44 + "function test() {\n"
45 + " var input = document.getElementById('myInput');\n"
46 + " input.setSelectionRange(2, 7);\n"
47 + " alert('hello');\n"
48 + "}\n"
49 + "</script></head>\n"
50 + "<body onload='test()'>\n"
51 + " <input id='myInput' value='some test'>\n"
52 + "</body></html>";
53
54 final String[] expected = getExpectedAlerts();
55 setExpectedAlerts(new String[] {expected[0]});
56 final HtmlPage page = loadPageWithAlerts(html);
57 final HtmlTextInput input = page.getHtmlElementById("myInput");
58
59 assertEquals(expected[1], input.getSelectedText());
60 }
61
62
63
64
65 @Test
66 @Alerts("initial")
67 public void focus() throws Exception {
68 final String html = DOCTYPE_HTML
69 + "<html><body>\n"
70 + "<iframe name='theFrame' src='" + URL_SECOND + "'></iframe>\n"
71 + "</body></html>";
72 final String frame = DOCTYPE_HTML
73 + "<html><body>\n"
74 + "<input id='input' value='initial' onfocus='alert(this.value)'>\n"
75 + "<div id='div'>click me</div>\n"
76 + "</body></html>";
77
78 getMockWebConnection().setResponse(URL_SECOND, frame);
79
80 final List<String> collectedAlerts = new ArrayList<>();
81 final HtmlPage page = loadPage(html, collectedAlerts);
82 final HtmlPage framePage = (HtmlPage) page.getFrames().get(0).getEnclosedPage();
83
84 framePage.getHtmlElementById("input").type("foo");
85
86 framePage.getHtmlElementById("div").click();
87 assertEquals(getExpectedAlerts(), collectedAlerts);
88 }
89 }