0
0
Selenium Javatesting~10 mins

Mouse hover (moveToElement) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium WebDriver
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.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();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is maximized and ready-PASS
2Navigates to https://example.com/menuPage with menu loaded-PASS
3Finds the menu item element by id 'menu-item'Menu item element is located on the page-PASS
4Performs mouse hover action on the menu item using Actions.moveToElement().perform()Mouse pointer is over the menu item, submenu should appear-PASS
5Finds the submenu item element by id 'submenu-item'Submenu element is located on the page-PASS
6Checks if submenu item is displayed using isDisplayed()Submenu is visible on the pageAssert.assertTrue(subMenu.isDisplayed()) verifies submenu visibilityPASS
7Test ends and browser closesBrowser closed, resources released-PASS
Failure Scenario
Failing Condition: Submenu element is not found or not visible after mouse hover
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to perform the mouse hover action?
AActions.moveToElement(element).perform()
BWebDriver.findElement()
Cdriver.get()
Delement.click()
Key Result
Always verify that the element you want to interact with is present and visible before performing actions like mouse hover. Use explicit waits if needed to ensure the page is ready.