0
0
Selenium Javatesting~15 mins

Mouse hover (moveToElement) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify mouse hover displays submenu
Preconditions (2)
Step 1: Locate the main menu item with text 'Services'
Step 2: Move the mouse pointer over the 'Services' menu item
Step 3: Observe if the submenu appears below the 'Services' menu
✅ Expected Result: The submenu under 'Services' is displayed when mouse hovers over the main menu item
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify submenu element is displayed after mouse hover
Best Practices:
Use Actions class for mouse hover (moveToElement)
Use explicit waits to wait for submenu visibility
Use meaningful locators like By.linkText or By.cssSelector
Avoid Thread.sleep; use WebDriverWait instead
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import org.junit.Assert;

public class MouseHoverTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

            // Locate the main menu item 'Services'
            WebElement servicesMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Services")));

            // Perform mouse hover using Actions
            Actions actions = new Actions(driver);
            actions.moveToElement(servicesMenu).perform();

            // Wait for submenu to be visible
            WebElement submenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.submenu")));

            // Assert submenu is displayed
            Assert.assertTrue("Submenu should be visible after hover", submenu.isDisplayed());

            System.out.println("Test Passed: Submenu displayed on mouse hover.");
        } finally {
            driver.quit();
        }
    }
}

This code opens the website and waits for the 'Services' menu item to be visible. It uses the Actions class to move the mouse pointer over the 'Services' menu. Then it waits explicitly for the submenu to appear using WebDriverWait and ExpectedConditions.visibilityOfElementLocated. Finally, it asserts that the submenu is displayed. The try-finally block ensures the browser closes after the test.

Using explicit waits avoids flaky tests and Actions.moveToElement is the correct way to simulate mouse hover in Selenium.

Common Mistakes - 3 Pitfalls
Using Thread.sleep instead of explicit waits
Using incorrect locator for submenu
Not using Actions class for mouse hover
Bonus Challenge

Now add data-driven testing to hover over three different main menu items and verify their submenus appear.

Show Hint