Mouse Actions in Selenium WebDriver

Mouse Actions in Selenium WebDriver

In this Selenium tutorial, we will be studying the advanced mouse interactions using Actions class. Using these methods, we can perform mouse operations like right-click, double click, mouse hover, click and hold, etc.

Actions Class Method for Mouse Interactions

  1. click()-
    This method is used to click at the current mouse pointer position. It is particularly useful when used with other mouse and keyboard events, generating composite actions.
  2. click(WebElement webElement)-
    This method is used to click at the middle of a web element passed as parameter to the click() method.
  3. clickAndHold()-
    The clickAndHold() method is used to perform the click method without releasing the mouse button.
  4. clickAndHold(WebElement onElement)-
    This method performs the click method without releasing the mouse button over a web element.
  5. contextClick()-
    This method is used to perform the right click operation(context-click) at the current mouse position.
  6. contextClick(WebElement onElement)-
    This method performs the right click operation at a particular web element.
  7. doubleClick()-
    As the name suggest, this method performs double click operation at a current mouse position.
  8. doubleClick(WebElement onElement)-
    Performs the double click operation at a particular web element.
  9. dragAndDrop(WebElement fromElement, WebElement toElement)-
    This is a utility method to perform the dragAndDrop operation directly wherein, we can pass the source element and the target element as parameter.
  10. dragAndDropBy(WebElement fromElement, int xOffset, int yOffset)-
    This method is a variation of dragAndDrop(fromElement, toElement) in which instead of passing the target element as parameter, we pass the x and y offsets. The method clicks the source web element and then releases at the x and y offsets.
  11. moveByOffset(int xOffset, int yOffset)-
    This method is used to move the mouse pointer to a particular position based on the x and y offsets passed as parameter.
  12. moveToElement(WebElement toElement)-
    This method is used to move the mouse pointer to a web element passed as parameter.
  13. moveToElement(WebElement toElement, int xOffset, int yOffset)-
    This method moves the mouse pointer by the given x and y offsets from the top-left corner of the specified web element.
  14. release()-
    This method releases the pressed left mouse button at the current mouse pointer position.
  15. release(WebElement onElement)-
    This method release the pressed left mouse button at a particular web element.

Code snippet for a Mouse Action

//WebElement which needs to be right clicked
WebElement rtClickElement = driver.findElement(By Locator of rtClickElement);
//Generating a Action to perform context click or right click
Actions rightClickAction = new Actions(driver).contextClick(rtClickElement);
//Performing the right click Action generated
rightClickAction.build().perform();

This completes our tutorial on Mouse Actions in Selenium WebDriver. For complete step by step Selenium WebDriver tutorial check – Selenium Tutorial.

От QA genius

Adblock
detector