Test Overview
This test performs a mouse hover action on a menu item using Selenium WebDriver in Java. It verifies that the submenu becomes visible after hovering.
This test performs a mouse hover action on a menu item using Selenium WebDriver in Java. It verifies that the submenu becomes visible after hovering.
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.junit.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class MouseHoverTest { private WebDriver driver; @Before public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://example.com/menu"); } @Test public void testMouseHoverShowsSubMenu() { WebElement menu = driver.findElement(By.id("menu-item")); Actions actions = new Actions(driver); actions.moveToElement(menu).perform(); WebElement subMenu = driver.findElement(By.id("submenu-item")); Assert.assertTrue(subMenu.isDisplayed()); } @After public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is maximized and ready | — | PASS |
| 2 | Navigates to https://example.com/menu | Page with menu loaded | — | PASS |
| 3 | Finds the menu item element by id 'menu-item' | Menu item element is located on the page | — | PASS |
| 4 | Performs mouse hover action on the menu item using Actions.moveToElement().perform() | Mouse pointer is over the menu item, submenu should appear | — | PASS |
| 5 | Finds the submenu item element by id 'submenu-item' | Submenu element is located on the page | — | PASS |
| 6 | Checks if submenu item is displayed using isDisplayed() | Submenu is visible on the page | Assert.assertTrue(subMenu.isDisplayed()) verifies submenu visibility | PASS |
| 7 | Test ends and browser closes | Browser closed, resources released | — | PASS |
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