0
0
Selenium Javatesting~15 mins

Double click in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify double click action on button triggers alert
Preconditions (2)
Step 1: Open the test web page URL
Step 2: Locate the button with id 'double-click-btn'
Step 3: Perform a double click on the button
Step 4: Switch to the alert that appears
Step 5: Capture the alert text
Step 6: Accept the alert
✅ Expected Result: An alert appears with text 'Button double clicked!' and is accepted successfully
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify alert is present after double click
Verify alert text equals 'Button double clicked!'
Best Practices:
Use Actions class for double click
Use explicit waits to wait for alert presence
Use By.id locator for button
Handle alert properly to avoid test hang
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 org.openqa.selenium.Alert;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DoubleClickTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("https://example.com/double-click-test");

            WebElement button = driver.findElement(By.id("double-click-btn"));

            Actions actions = new Actions(driver);
            actions.doubleClick(button).perform();

            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();

            String alertText = alert.getText();
            assertEquals("Button double clicked!", alertText);

            alert.accept();
        } finally {
            driver.quit();
        }
    }
}

The code starts by setting up the ChromeDriver and opening the test page URL.

It locates the button using By.id("double-click-btn"), which is a reliable locator.

The Actions class is used to perform a double click on the button.

After the double click, it waits explicitly for the alert to appear using WebDriverWait and ExpectedConditions.alertIsPresent().

Once the alert is present, it switches to the alert, captures the text, and asserts that it matches the expected message.

Finally, it accepts the alert to close it and quits the browser to clean up.

Common Mistakes - 4 Pitfalls
Using click() twice instead of doubleClick()
Not waiting for alert before switching
Using XPath with absolute path for button locator
Not closing the alert after verification
Bonus Challenge

Now add data-driven testing with 3 different buttons that trigger different alerts on double click

Show Hint