Some interactions can't be done with simple click() and sendKeys(). Hover over a menu to reveal a submenu. Right-click for a context menu. Drag and drop an element. Press Ctrl+A to select all. The Actions class handles all of these.
import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
// Hover over an element (reveal dropdown menu)
WebElement menu = driver.findElement(By.cssSelector(".nav-menu"));
actions.moveToElement(menu).perform();
// Right-click (context menu)
WebElement target = driver.findElement(By.id("targetElement"));
actions.contextClick(target).perform();
// Double-click
actions.doubleClick(target).perform();
// Drag and drop
WebElement source = driver.findElement(By.id("dragSource"));
WebElement dropZone = driver.findElement(By.id("dropTarget"));
actions.dragAndDrop(source, dropZone).perform();import org.openqa.selenium.Keys;
Actions actions = new Actions(driver);
// Press Enter
actions.sendKeys(Keys.ENTER).perform();
// Tab to next field
actions.sendKeys(Keys.TAB).perform();
// Ctrl+A (select all)
actions.keyDown(Keys.CONTROL)
.sendKeys("a")
.keyUp(Keys.CONTROL)
.perform();
// Ctrl+C (copy), Ctrl+V (paste)
actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
actions.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();
// Escape key (close a modal)
actions.sendKeys(Keys.ESCAPE).perform();Screenshots are essential for debugging failed tests. When a test fails at 2 AM on Jenkins, the screenshot tells you what the page looked like at the moment of failure.
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
// Full page screenshot
File screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
Files.copy(screenshot.toPath(),
Path.of("screenshots/full-page.png"),
StandardCopyOption.REPLACE_EXISTING);
// Screenshot of a specific element only
WebElement loginForm = driver.findElement(By.tagName("form"));
File elementShot = loginForm.getScreenshotAs(OutputType.FILE);
Files.copy(elementShot.toPath(),
Path.of("screenshots/login-form.png"),
StandardCopyOption.REPLACE_EXISTING);
// As Base64 string (for embedding in reports)
String base64 = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.BASE64);In real projects, you take screenshots automatically on test failure using a TestNG listener. We'll cover this in the TestNG chapter. For now, just know that TakesScreenshot is how you capture them.
Q: What is the Actions class used for?
A: The Actions class handles complex user interactions that simple click() and sendKeys() can't do — mouse hover, right-click (context click), double-click, drag-and-drop, and keyboard shortcuts (Ctrl+C, Ctrl+V). You build a chain of actions and call perform() to execute them. For example, hovering over a navigation menu to reveal a dropdown submenu.
Q: How do you take a screenshot in Selenium?
A: Cast the driver to TakesScreenshot and call getScreenshotAs(OutputType.FILE) for a file, or OutputType.BASE64 for a base64 string. You can also take element-level screenshots by calling getScreenshotAs() on a WebElement. In our framework, we capture screenshots automatically on test failure using a TestNG listener and attach them to Allure reports.
Key Point: Actions class handles hover, right-click, drag-drop, and keyboard shortcuts. TakesScreenshot captures the page or a specific element.