07. Generating Mouse Hover Event On Main Menu To Click On Sub Menu

To generate mouse hover event in webdriver software testing tool, We can use advanced user interactions API constructor "Actions" with "moveToElement" method. Bunch of syntax to hover mouse on main menu is as bellow. You can replace xpath of an element as per your requirement in bellow given syntax

Actions actions = new Actions(driver);
WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div")); actions.moveToElement(moveonmenu);
actions.perform();


Above example will only move mouse on targeted element of software web application. It will not perform click operation. To perform click operation on sub menu, we need to use click() action before perform() as shown in bellow example.


package Others;
import java.util.concurrent.TimeUnit;
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 org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Dynamicalert
{
            WebDriver driver;
            @BeforeTest
            public void setup() throws Exception
            {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();                       
                        driver.manage().window().maximize();                     
                        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                        driver.get("http://only-testing-blog.blogspot.in/p/mouse-hover.html");
            }
            @AfterTest
            public void tearDown() throws Exception
            {
                        driver.quit();
            }
            @Test
            public void Text() throws InterruptedException
            {
                        Actions actions = new Actions(driver);
                        WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div"));
                        actions.moveToElement(moveonmenu).moveToElement(driver.findElement(By.xpath("//div[@id='menu1choices']/a"))).click().perform();
                        WebDriverWait wait = new WebDriverWait(driver, 15);
                        wait.until(ExpectedConditions.titleContains("Google"));
                       
            }
           
           
}


No comments:

Post a Comment