0
0
Selenium Javatesting~15 mins

Click and hold in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify click and hold action on a button
Preconditions (1)
Step 1: Locate the button with id 'hold-button'
Step 2: Perform click and hold action on the button for 3 seconds
Step 3: Release the click
Step 4: Verify that the button text changes to 'Held!' after the hold
✅ Expected Result: The button text changes to 'Held!' after clicking and holding for 3 seconds
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the button text changes to 'Held!' after click and hold
Best Practices:
Use Actions class for click and hold
Use explicit waits to wait for text change
Use By.id locator for the button
Avoid Thread.sleep; use WebDriverWait instead
Automated Solution
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

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/testpage");

            WebElement button = driver.findElement(By.id("hold-button"));

            Actions actions = new Actions(driver);
            actions.clickAndHold(button).pause(Duration.ofSeconds(3)).release().perform();

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
            wait.until(ExpectedConditions.textToBe(By.id("hold-button"), "Held!"));

            String buttonText = button.getText();
            assertEquals("Held!", buttonText, "Button text should be 'Held!' after click and hold");

            System.out.println("Test passed: Button text changed to 'Held!'");
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate the click and hold action.

First, it opens the test page and locates the button by its id 'hold-button'.

Then, it uses the Actions class to perform a click and hold on the button for 3 seconds, then releases the click.

After that, it waits explicitly for the button text to change to 'Held!' using WebDriverWait and ExpectedConditions.textToBe.

Finally, it asserts that the button text is exactly 'Held!'.

The test prints a success message if the assertion passes and closes the browser in the finally block to ensure cleanup.

Common Mistakes - 4 Pitfalls
Using Thread.sleep instead of explicit waits
Using XPath with absolute paths for locating the button
Not releasing the click after click and hold
Not handling exceptions or quitting the driver
Bonus Challenge

Now add data-driven testing with 3 different hold durations: 1 second, 3 seconds, and 5 seconds, verifying the button text changes accordingly.

Show Hint