1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.htmlunit.SimpleWebTestCase;
21 import org.htmlunit.junit.BrowserRunner;
22 import org.htmlunit.junit.annotation.Alerts;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25
26
27
28
29
30
31
32
33 @RunWith(BrowserRunner.class)
34 public class HtmlSubmitInput2Test extends SimpleWebTestCase {
35
36
37
38
39 @Test
40 @Alerts({})
41 public void defaultValue() throws Exception {
42 final String html = DOCTYPE_HTML
43 + "<html><head>\n"
44 + "<script>\n"
45 + " function test() {\n"
46 + " alert(document.getElementById('myId').value);\n"
47 + " }\n"
48 + "</script>\n"
49 + "</head>\n"
50 + "<body onload='test()'>\n"
51 + "<form action='foo.html'>\n"
52 + " <input type='submit' id='myId'>\n"
53 + "</form>\n"
54 + "</body></html>";
55
56 final HtmlPage page = loadPageWithAlerts(html);
57 assertTrue(page.asNormalizedText().indexOf("Submit Query") > -1);
58 assertFalse(page.asXml().indexOf("Submit Query") > -1);
59 }
60
61
62
63
64 @Test
65 @Alerts("")
66 public void emptyValue() throws Exception {
67 final String html = DOCTYPE_HTML
68 + "<html><head>\n"
69 + "<script>\n"
70 + " function test() {\n"
71 + " alert(document.getElementById('myId').value);\n"
72 + " }\n"
73 + "</script>\n"
74 + "</head>\n"
75 + "<body onload='test()'>\n"
76 + "<form action='" + URL_SECOND + "'>\n"
77 + " <input type='submit' id='myId' value=''>\n"
78 + "</form>\n"
79 + "</body></html>";
80
81 final HtmlPage page = loadPageWithAlerts(html);
82 assertFalse(page.asNormalizedText().indexOf("Submit Query") > -1);
83 assertTrue(page.asXml().indexOf("value=\"\"") > -1);
84 }
85
86
87
88
89 @Test
90 @Alerts("1")
91 public void onclick() throws Exception {
92 final String html = DOCTYPE_HTML
93 + "<html><head></head>\n"
94 + "<body>\n"
95 + "<form>\n"
96 + " <input id='myInput'>\n"
97 + " <input type='submit' onclick='alert(1)'>\n"
98 + "</form>\n"
99 + "</body></html>";
100
101 final List<String> collectedAlerts = new ArrayList<>();
102 final HtmlPage page = loadPage(html, collectedAlerts);
103 page.getHtmlElementById("myInput").type('\n');
104
105 assertEquals(getExpectedAlerts(), collectedAlerts);
106 }
107
108
109
110
111
112
113 @Test
114 public void asXmlNoEscape() throws Exception {
115 final String html = DOCTYPE_HTML
116 + "<html><head>\n"
117 + "<meta http-equiv='Content-Type' content='text/html; charset=Cp1251'>\n"
118 + "</head><body>\n"
119 + "<input type='submit' value='л'/>\n"
120 + "<input type='reset' value='л'/>\n"
121 + "</body></html>";
122
123 final HtmlPage page = loadPage(html);
124 final String xml = page.asXml();
125 assertTrue("\u043B" + xml, xml.contains("<input type=\"submit\" value=\"\u043B\"/>"));
126 assertTrue(xml, xml.contains("<input type=\"reset\" value=\"\u043B\"/>"));
127 }
128 }