Selenium WeDriver Advanced Commands - Actions Class Mouse Events

In this tutorial we will learn Mouse event commands of Actions class of Selenium Webdriver.

 Mouse Events - Some of the mouse events that can be performed using Actions class are:
  • click()
  • doubleClick()
  • contextClick()
  • clickAndHold()
  • dragAndDrop()
  • moveToElement()
  • moveByOffset(x, y)
  • release()

1. Mouse Hover & Click
We usually come across some scenarios in automation of mouse hover like clicking on a sub-menu link which appears only after moving your cursor to menu link. It's quite a tricky thing to perform in automation. Thanks to the Actions Class in Selenium Webdriver.  We already have a Selenium command for it.
Code for Mouse Hover:
/* Mouse Hover
 */

// Xpath for Menu
WebElement Menu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Xpath for Sub Menu
WebElement SubMenu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of Actions class
Actions actions = new Actions(driver);

// Move cursor to Menu link (Mouse hover on menu link so that sub menu is displayed)
actions.moveToElement(Menu_Link);

// Click on Submenu link (whcih is displayed after mouse hovering curson on menu link)
actions.click(TSHIRTS_Submenu_Link).build().perform();
 

Explanation of above code:
  1. Creating object of Actions class as actions. And passing driver object to constructor of Actions class.
  2. In second line, creating an object of WebElement and passing Xpath locator to it.
  3. moveToElement() is the command to perform mouse hover action.
  4. actions.build().perform() - This code is mandatory to finish any actions class command. build() method combines multiple actions into one single action and perform() executes those  actions.

2. Double Click
This command is used to double click on an element.

Code for Double Click:
/* Double Click
 */
Actions actions = new Actions(driver);
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));
actions.doubleClick(PRODUCT_CATEGORY);
actions.build().perform();


3. Context Click (Right Click)
This command is used to right click on an element.


Code for Context click (right click):
/* Right Click
 */

// Locate web element
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of actions class
Actions actions = new Actions(driver);

// Right click on PRODUCT_CATEGORY element
actions.contextClick(PRODUCT_CATEGORY);
actions.build().perform();


4. Drag and Drop
This command is for drag and drop a web element and is rarely used.


Code for Drag and Drop:
/*   Drag And Drop
 */

// Weblements for source and target
WebElement source = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

// Create object of actions class
Actions action = new Actions(driver);
action.dragAndDrop(source, target);
action.build().perform();


Refer Selenium Webdriver Tutorials Series

Happy Learning!

Comments

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Selenium Essentials - How to become expert in Automation Testing?

Top 25 Must to know Selenium Webdriver Commands List

Selenium WebDriver - Browser and Navigation Commands

Selenium IDE - Sample Script With Most Commonly Used Commands