View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link HtmlSubmitInput}.
28   *
29   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
30   * @author Marc Guillemot
31   * @author Ahmed Ashour
32   */
33  @RunWith(BrowserRunner.class)
34  public class HtmlSubmitInput2Test extends SimpleWebTestCase {
35  
36      /**
37       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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      * Test that attributes values are not escaped
110      * See bug 3074609.
111      * @throws Exception if the test fails
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='&#1083;'/>\n"
120             + "<input type='reset' value='&#1083;'/>\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 }