0
0
Selenium Javatesting~5 mins

Click and hold in Selenium Java

Choose your learning style9 modes available
Introduction

Click and hold lets you press and keep holding a mouse button on a web element. This helps test actions like dragging or selecting.

When testing drag-and-drop features on a webpage.
When you need to simulate selecting text or multiple items by holding the mouse button.
When verifying that a button reacts correctly while being pressed and held.
When testing sliders or controls that respond to click-and-hold actions.
When checking if a tooltip or menu appears after holding a click.
Syntax
Selenium Java
Actions actions = new Actions(driver);
actions.clickAndHold(element).perform();
Use the Actions class from Selenium to perform complex mouse actions.
Always call perform() to execute the action sequence.
Examples
Click and hold on a button with id 'button1'.
Selenium Java
Actions actions = new Actions(driver);
WebElement button = driver.findElement(By.id("button1"));
actions.clickAndHold(button).perform();
Click and hold a slider, move it 50 pixels to the right, then release.
Selenium Java
Actions actions = new Actions(driver);
WebElement slider = driver.findElement(By.cssSelector(".slider"));
actions.clickAndHold(slider).moveByOffset(50, 0).release().perform();
Sample Program

This test opens a page with drag-and-drop elements. It clicks and holds the draggable element, moves it over the droppable area, and releases. Then it checks if the drop was successful by reading the target's text.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ClickAndHoldTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/drag_and_drop");
            WebElement source = driver.findElement(By.id("draggable"));
            WebElement target = driver.findElement(By.id("droppable"));

            Actions actions = new Actions(driver);
            actions.clickAndHold(source).moveToElement(target).release().perform();

            String text = target.getText();
            if (text.equals("Dropped!")) {
                System.out.println("Test Passed: Drag and drop succeeded.");
            } else {
                System.out.println("Test Failed: Drag and drop did not work.");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Make sure the element is visible and enabled before clicking and holding.

Use perform() to execute the action chain; otherwise, nothing happens.

Combine clickAndHold with moveToElement and release for drag-and-drop tests.

Summary

Click and hold simulates pressing and holding the mouse button on an element.

It is useful for testing drag-and-drop and selection features.

Always use Actions class and call perform() to run the action.