Action class in Selenium is a collection of individual actions that user wants to perform e.g. a mouse click, drag and drop events, pressing a keyboard key.
Action class is a built-in feature for handling keyboard and mouse events. All the operations from the action class are performed using the advanced user interaction API in Selenium Webdriver.
Action class is defined and invoked using the following syntax:
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Methods in Action class can be mainly categorized into Mouse events and Keyboard events.
keyDown(modifier key)
— Performs a modifier key press and subsequent interactions may assume it’s kept pressed.sendKeys(keys to send)
— Sends keys to the active web element, usually a text field.keyUp(modifier key)
— Performs a modifier key release.click()
— Performs click at the current mouse location.doubleClick()
— Performs double click at the current mouse location.contextClick()
— Performs right-click on the mouse.clickAndHold()
— Clicks (without releasing) in the middle of the given element.dragAndDrop(source, target)
— Click-and-hold at the location of the source element, and moves to the location of the target element.dragAndDropBy(source, xOffset, yOffset)
— Click-and-hold at the location of the source element, moves by a given offset.moveByOffset(x-offset, y-offset)
— Moves the mouse from its current position (or 0,0) by the given offset.moveToElement(toElement)
— Moves the mouse to the middle of the element.release()
— Releases the depressed left mouse button at the current mouse location.Test Scenario: Visit a page and click on a button
driver.get("Page URL");
Actions action = new Actions(driver);
element = driver.findElement(By.linkText("text on the button"));
action.moveToElement(element).click();
Test Scenario: Perform mouse hover on an element
driver.get("Page URL");
Actions action = new Actions(driver);
WebElement element = driver.findElement(By. xPath("xpath of web element"));
action.moveToElement(element).build().perform();
Test Scenario: Perform double click action on a button
driver.get("Page URL");
Actions action = new Actions(driver);
WebElement element = driver.findElement(By. xPath("xpath of web element"));
action.doubleClick(element).perform();
Source: https://www.guru99.com/keyboard-mouse-events-files-webdriver.html
Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.