QA
Junior QASeleniumFree question

What is the Actions class? Give examples.

60-second answer

The Actions class handles advanced user interactions that a simple click or sendKeys can't, such as mouse hover, right-click, double-click, drag and drop, click-and-hold, and keyboard combinations like Ctrl+A or Shift+typing. I create new Actions with the driver, chain the steps, and call perform at the end. A common use is hovering over a menu to reveal a submenu and then clicking an item, or dragging a card from one column to another on a board.

Explanation

Methods: moveToElement, contextClick, doubleClick, dragAndDrop, dragAndDropBy, clickAndHold, release, keyDown, keyUp, sendKeys, scrollToElement (Selenium 4), pause.

Always end with .perform(); without it, nothing happens. build() is optional in Selenium 4.

If HTML5 drag and drop doesn't work with Actions (a known limitation in some apps), use JavaScript or the app's API.

Example

Hover on "Electronics" in the nav bar, then click "Laptops" in the submenu.

Sample code

Actions actions = new Actions(driver);

actions.moveToElement(driver.findElement(By.linkText("Electronics")))
       .click(driver.findElement(By.linkText("Laptops")))
       .perform();

actions.contextClick(driver.findElement(By.id("file"))).perform();   // right-click
actions.doubleClick(driver.findElement(By.id("name"))).perform();
actions.dragAndDrop(driver.findElement(By.id("card-1")),
                    driver.findElement(By.id("done-column"))).perform();
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();