Your ultimate guide

Handling Mouse Actions/Mouse Over using Selenium with Java

 Mouse Over:

    When we want to use Mouse or Keyboard actions we should create an "Actions" class object.

        Syntax: Actions object = new Actions(browser_object);
        Example: Actions act = new Actions(driver);

Method for Actions Class:

    build():- Build method in the Actions class is used to create a chain of actions or operations you want to perform.

    perform():- Perform Method in Action class is used to Execute a chain of actions that are built using the Action build method.




moveToElement(): This method is used to Move the Mouse point to a Particular Web Element.
Syntax: 
    Actions action_obj = new Actions(driver);
    WebElement ele = driver.findElement(By.id("123"));
    actions_obj.moveToElement("ele");

Example: 
class MousePnt
{
    public static void main(String args[])
    {
        System.setProperty("webdriver.chrome.driver" , "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        
        // Create object instance for actions class
        Actions act = new Actions(driver);

        WebElement searchBox = driver.findElement(By.name("q"));
        act.moveElement(searchBox).perform();
        //act.moveElement(searchBox).click().perform();
    }
}


doubleClick(): This method is used to double-click on a particular web element.
    Syntax: actions_obj.doubleClick(WebElement).perform();

Example:
class DoubleClick
{
    public static void main(String args[])
    {
        System.setProperty("webdriver.chrome.driver" , "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://demoqa.com/buttons");
        Actions actn = new Actions(driver);
        
        WebDriver doubleClkBtn = driver.findElement(By.id("doubleClickBtn"));
        actn.doubleClick(doubleClkBtn).perform();
    }
}



contextClick(): To perform a (Mouse)Right-Click operation on a particular Web Element.
    Syntax: action_obj.contextClick(WebElement).perform();

Example:
class RightClick
{
    public static void main(String args[])
    {
        System.setProperty("webdriver.chrome.driver" , "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://testingcolleges.blogspot.com");
        Actions actn = new Actions(driver);

        WebElement rClick = driver.findElement(By.name("q"));
        actn.contextClick(rClick).perform();
        //(or) actn.moveToElement(rClick).contextClick().perform();
    }
}
       
        


Handling mouse key hold & release:
    We can use the following methods to perform hold & release Keyword keys and Mouse keys.
    Syntax: action.clickAndHold(elementName).perform();
                 action.release(elementName).perform();


Handling Drag & Drop: 
    Some Web applications have the functionality to drag web elements and drop them on a defined area or element. We can automate the drag and drop of such elements using the Selenium web driver.

Syntax: action.dragAndDrop(SourceLocator, DestinationLocator);

Example:
class HandlingDragAndDrop
{
    public static void main(String args[])
    {
        System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String url = "https://example.com";
        driver.get(url)
        
        WebElement dragMe = driver.findElement(By.id("123"));
        WebElement dropMe = driver.findElement(By.id("456"));

        Actions action = new Actions(driver);
        action.dragAndDrop(dragMe, dropMe).perform();
    }
}

- Next Page: Keyboard_Interactions

No comments:

Post a Comment