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.annotation.Alerts;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link HtmlSubmitInput}.
26   *
27   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
28   * @author Marc Guillemot
29   * @author Ahmed Ashour
30   */
31  public class HtmlSubmitInput2Test extends SimpleWebTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({})
38      public void defaultValue() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + "  function test() {\n"
43              + "    alert(document.getElementById('myId').value);\n"
44              + "  }\n"
45              + "</script>\n"
46              + "</head>\n"
47              + "<body onload='test()'>\n"
48              + "<form action='foo.html'>\n"
49              + "  <input type='submit' id='myId'>\n"
50              + "</form>\n"
51              + "</body></html>";
52  
53          final HtmlPage page = loadPageWithAlerts(html);
54          assertTrue(page.asNormalizedText().indexOf("Submit Query") > -1);
55          assertFalse(page.asXml().indexOf("Submit Query") > -1);
56      }
57  
58      /**
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts("")
63      public void emptyValue() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><head>\n"
66              + "<script>\n"
67              + "  function test() {\n"
68              + "    alert(document.getElementById('myId').value);\n"
69              + "  }\n"
70              + "</script>\n"
71              + "</head>\n"
72              + "<body onload='test()'>\n"
73              + "<form action='" + URL_SECOND + "'>\n"
74              + "  <input type='submit' id='myId' value=''>\n"
75              + "</form>\n"
76              + "</body></html>";
77  
78          final HtmlPage page = loadPageWithAlerts(html);
79          assertFalse(page.asNormalizedText().indexOf("Submit Query") > -1);
80          assertTrue(page.asXml().indexOf("value=\"\"") > -1);
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("1")
88      public void onclick() throws Exception {
89          final String html = DOCTYPE_HTML
90              + "<html><head></head>\n"
91              + "<body>\n"
92              + "<form>\n"
93              + "  <input id='myInput'>\n"
94              + "  <input type='submit' onclick='alert(1)'>\n"
95              + "</form>\n"
96              + "</body></html>";
97  
98          final List<String> collectedAlerts = new ArrayList<>();
99          final HtmlPage page = loadPage(html, collectedAlerts);
100         page.getHtmlElementById("myInput").type('\n');
101 
102         assertEquals(getExpectedAlerts(), collectedAlerts);
103     }
104 
105     /**
106      * Test that attributes values are not escaped
107      * See bug 3074609.
108      * @throws Exception if the test fails
109      */
110     @Test
111     public void asXmlNoEscape() throws Exception {
112         final String html = DOCTYPE_HTML
113             + "<html><head>\n"
114             + "<meta http-equiv='Content-Type' content='text/html; charset=Cp1251'>\n"
115             + "</head><body>\n"
116             + "<input type='submit' value='&#1083;'/>\n"
117             + "<input type='reset' value='&#1083;'/>\n"
118             + "</body></html>";
119 
120         final HtmlPage page = loadPage(html);
121         final String xml = page.asXml();
122         assertTrue("\u043B" + xml, xml.contains("<input type=\"submit\" value=\"\u043B\"/>"));
123         assertTrue(xml, xml.contains("<input type=\"reset\" value=\"\u043B\"/>"));
124     }
125 }