1 package com.atlassian.webdriver.utils;
2
3 import com.atlassian.webdriver.AtlassianWebDriver;
4 import org.openqa.selenium.By;
5 import org.openqa.selenium.Dimension;
6 import org.openqa.selenium.Point;
7 import org.openqa.selenium.WebDriver;
8 import org.openqa.selenium.WebElement;
9 import org.openqa.selenium.interactions.Actions;
10
11 /**
12 * Implements mouse events that are not handled correctly in WebDriver atm.
13 */
14 public class MouseEvents
15 {
16
17 private MouseEvents() {}
18
19 /**
20 * Fires a mouse over event on an element that matches the By
21 *
22 * @param by the element matcher to apply the hover to.
23 * @return the {@link WebElement} that the hover was triggered on.
24 */
25 public static WebElement hover(By by, WebDriver driver)
26 {
27 return hover(driver.findElement(by), driver);
28 }
29
30 /**
31 * Fires a mouse over event on the specified element.
32 *
33 * @param el The element to fire the hover event on.
34 *
35 * @return the {@link WebElement} the hover was triggered on.
36 */
37 public static WebElement hover(WebElement el, WebDriver driver)
38 {
39 Actions hover = new Actions(driver).moveToElement(el);
40 hover.perform();
41
42 return el;
43 }
44
45 /**
46 * Fires a mouse out event on the element that matches the By.
47 *
48 * @param by the element matcher to apply the hover to.
49 *
50 * @return the {@link WebElement} the mouseout was fired on.
51 */
52 public static WebElement mouseout(By by, WebDriver driver)
53 {
54 return mouseout(driver.findElement(by), driver);
55 }
56
57 /**
58 * Fires a mouse out event on the element.
59 *
60 * @param el the element to fire the mouseout event on.
61 *
62 * @return the {@link WebElement} the mouseout was fired on.
63 */
64 public static WebElement mouseout(WebElement el, WebDriver driver)
65 {
66 // native mouseout event not supported in Firefox yet.
67 if (WebDriverUtil.isFirefox(driver))
68 {
69 JavaScriptUtils.dispatchMouseEvent("mouseout", el, driver);
70 }
71 else
72 {
73 // Move to the element and then move away to the body element.
74 Actions actions = new Actions(driver).moveToElement(el)
75 .moveToElement(driver.findElement(By.tagName("body")));
76 actions.perform();
77 }
78
79 return el;
80 }
81
82 }