Verify mouse hover displays submenu
Preconditions (2)
✅ Expected Result: The submenu under 'Services' is displayed when mouse hovers over the main menu item
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.
Now add data-driven testing to hover over three different main menu items and verify their submenus appear.
moveToElement() in Selenium WebDriver?moveToElement() method is used to move the mouse pointer over a specific web element, simulating a hover action.click(), scrollTo(), and sendKeys(). So, moveToElement() is specifically for hover.menu using Selenium Actions in Java?new Actions(driver), then chain moveToElement(element) and finish with perform().WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.moveToElement(menu).perform();
String tooltip = driver.findElement(By.id("tooltip")).getText();
System.out.println(tooltip);moveToElement(menu).perform() simulates mouse hover on the menu element, which triggers the tooltip to appear.Actions actions = new Actions(driver); actions.moveToElement(menu);
moveToElement() build the action but do not execute it until perform() is called.perform(), so the hover action is never executed.moveToElement(menu) to hover, then click(submenu) to click the submenu, and finally perform() to execute both actions in order.Pass the test and this pattern moves unknown