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.WebDriver;
24  import org.openqa.selenium.interactions.Actions;
25  
26  /**
27   * Tests for various clickable elements.
28   *
29   * @author David K. Taylor
30   * @author Chris Erskine
31   * @author Marc Guillemot
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @RunWith(BrowserRunner.class)
36  public class ClickableElement2Test extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts("1")
43      public void clickOnFocus() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head><script>" + LOG_TITLE_FUNCTION + "</script></head><body>\n"
46              + "<form>\n"
47              + "  <input type='button' id='textfield1' onfocus='log(1)'>\n"
48              + "</form>\n"
49              + "</body></html>";
50  
51          final WebDriver driver = loadPage2(html);
52          driver.findElement(By.id("textfield1")).click();
53  
54          verifyTitle2(driver, getExpectedAlerts());
55      }
56  
57      /**
58       * @throws Exception if the test fails
59       */
60      @Test
61      @Alerts({"click", "click", "dblclick"})
62      public void dblClick() throws Exception {
63          final String content = DOCTYPE_HTML
64              + "<html>\n"
65              + "<head>\n"
66              + "<script>\n"
67              + LOG_TEXTAREA_FUNCTION
68              + "  function clickMe() {\n"
69              + "    log('click');\n"
70              + "  }\n"
71              + "  function dblClickMe() {\n"
72              + "    log('dblclick');\n"
73              + "  }\n"
74              + "</script>\n"
75              + "</head>\n"
76              + "<body id='myBody' onclick='clickMe()' ondblclick='dblClickMe()'>\n"
77              + LOG_TEXTAREA
78              + "</body></html>";
79  
80          final WebDriver driver = loadPage2(content);
81  
82          final Actions action = new Actions(driver);
83          action.doubleClick(driver.findElement(By.id("myBody")));
84          action.perform();
85  
86          verifyTextArea2(driver, getExpectedAlerts());
87      }
88  }