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 org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.openqa.selenium.By;
23  import org.openqa.selenium.ElementNotInteractableException;
24  import org.openqa.selenium.InvalidElementStateException;
25  import org.openqa.selenium.Keys;
26  import org.openqa.selenium.WebDriver;
27  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
28  
29  /**
30   * Tests for {@link DomElement}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @RunWith(BrowserRunner.class)
36  public final class DomElementTest extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts({"2", "2"})
43      public void getElementsByTagName() throws Exception {
44          final String html = DOCTYPE_HTML
45                  + "<html><head>\n"
46                  + "<script>\n"
47                  + LOG_TITLE_FUNCTION
48                  + "function test() {\n"
49                  + "  log(document.f1.getElementsByTagName('input').length);\n"
50                  + "  log(document.f1.getElementsByTagName('INPUT').length);\n"
51                  + "}\n"
52                  + "</script></head>\n"
53                  + "<body onload='test()'>\n"
54                  + "  <form name='f1'>\n"
55                  + "    <input>\n"
56                  + "    <INPUT>\n"
57                  + "  </form>\n"
58                  + "</body></html>";
59  
60          final WebDriver driver = loadPageVerifyTitle2(html);
61          if (driver instanceof HtmlUnitDriver) {
62              final HtmlPage page = (HtmlPage) getEnclosedPage();
63              assertEquals(2, page.getForms().get(0).getElementsByTagName("input").size());
64              assertEquals(2, page.getForms().get(0).getElementsByTagName("INPUT").size());
65          }
66      }
67  
68      /**
69       * @throws Exception on test failure
70       */
71      @Test(expected = ElementNotInteractableException.class)
72      public void clickInvisible() throws Exception {
73          final String html = DOCTYPE_HTML
74                  + "<html>\n"
75                  + "<body>\n"
76                  + "  <a id='link' style='display: none'>Click me</a>\n"
77                  + "</body></html>";
78  
79          final WebDriver driver = loadPage2(html);
80          driver.findElement(By.id("link")).click();
81      }
82  
83      /**
84       * @throws Exception if an error occurs
85       */
86      @Test
87      @Alerts({"false", "true"})
88      public void clickFromJavaScript() throws Exception {
89          final String html = "<head>\n"
90              + "<script>\n"
91              + LOG_TITLE_FUNCTION
92              + " function test() {\n"
93              + "   try {\n"
94              + "     var e = document.getElementById('id1');\n"
95              + "     log(e.checked);\n"
96              + "     e.click();\n"
97              + "     log(e.checked);\n"
98              + "   } catch(e) {log(e)}\n"
99              + " }\n"
100             + "</script>\n"
101             + "</head>\n"
102             + "<body onload='test()'>\n"
103             + "  <div style='display: none;'>\n"
104             + "    <input type='checkbox' id='id1'>\n"
105             + "  </div>\n"
106             + "</body>\n";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception on test failure
113      */
114     @Test
115     public void clickDisabled() throws Exception {
116         final String html = DOCTYPE_HTML
117                 + "<html>\n"
118                 + "<body>\n"
119                 + "  <button id='id1' disabled>Click Me</button>\n"
120                 + "</body></html>";
121 
122         final WebDriver driver = loadPage2(html);
123         driver.findElement(By.id("id1")).click();
124     }
125 
126     /**
127      * @throws Exception on test failure
128      */
129     @Test(expected = InvalidElementStateException.class)
130     public void sendKeysToDisabled() throws Exception {
131         final String html = DOCTYPE_HTML
132                 + "<html>\n"
133                 + "<body>\n"
134                 + "  <input id='id1' disabled>\n"
135                 + "</body></html>";
136 
137         final WebDriver driver = loadPage2(html);
138         driver.findElement(By.id("id1")).sendKeys("Hello");
139     }
140 
141     /**
142      * @throws Exception on test failure
143      */
144     @Test
145     public void sendEnterKeyWithHiddenSubmit() throws Exception {
146         final String html = DOCTYPE_HTML
147             + "<html><head></head>\n"
148             + "<body>\n"
149             + "  <form id='myForm' action='" + URL_SECOND + "'>\n"
150             + "    <input id='myText' type='text'>\n"
151             + "    <input id='myButton' type='submit' style='display: none;'>Submit</input>\n"
152             + "  </form>\n"
153             + "</body></html>";
154         final String secondContent = "second content";
155 
156         getMockWebConnection().setResponse(URL_SECOND, secondContent);
157 
158         final WebDriver driver = loadPage2(html);
159         driver.findElement(By.id("myText")).sendKeys(Keys.ENTER);
160         if (useRealBrowser()) {
161             Thread.sleep(400);
162         }
163 
164         assertEquals(2, getMockWebConnection().getRequestCount());
165         assertEquals(URL_SECOND, getMockWebConnection().getLastWebRequest().getUrl());
166     }
167 
168     /**
169      * @throws Exception on test failure
170      */
171     @Test
172     public void sendEnterKey() throws Exception {
173         final String html = DOCTYPE_HTML
174             + "<html><head></head>\n"
175             + "<body>\n"
176             + "  <form id='myForm' action='" + URL_SECOND + "'>\n"
177             + "    <input id='myText' type='text'>\n"
178             + "  </form>\n"
179             + "</body></html>";
180         final String secondContent = "second content";
181 
182         getMockWebConnection().setResponse(URL_SECOND, secondContent);
183 
184         final WebDriver driver = loadPage2(html);
185         driver.findElement(By.id("myText")).sendKeys(Keys.ENTER);
186         if (useRealBrowser()) {
187             Thread.sleep(400);
188         }
189 
190         assertEquals(2, getMockWebConnection().getRequestCount());
191         assertEquals(URL_SECOND, getMockWebConnection().getLastWebRequest().getUrl());
192     }
193 
194     /**
195      * @throws Exception on test failure
196      */
197     @Test(expected = ElementNotInteractableException.class)
198     public void clickHiddenSubmit() throws Exception {
199         final String html = DOCTYPE_HTML
200             + "<html><head></head>\n"
201             + "<body>\n"
202             + "  <form id='myForm' action='" + URL_SECOND + "'>\n"
203             + "    <input id='myButton' type='submit' style='display: none;'>Submit</input>\n"
204             + "  </form>\n"
205             + "</body></html>";
206         final String secondContent = "second content";
207 
208         getMockWebConnection().setResponse(URL_SECOND, secondContent);
209 
210         final WebDriver driver = loadPage2(html);
211         driver.findElement(By.id("myButton")).click();
212     }
213 
214 }