Verify double click action on button triggers alert
Preconditions (2)
✅ Expected Result: An alert appears with text 'Button double clicked!' and is accepted successfully
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.
Now add data-driven testing with 3 different buttons that trigger different alerts on double click
doubleClick() method in Selenium WebDriver's Actions class?Actions class roleActions class in Selenium is used to build complex user interactions like mouse and keyboard events.doubleClick() method functiondoubleClick() method specifically performs a double-click on the targeted web element.Actions object with the driver, then chain the doubleClick() method with the target element, and call perform() to execute.new Actions(driver).doubleClick(element).perform();. Others are invalid or incorrect method calls.WebElement element = driver.findElement(By.id("btn"));
new Actions(driver).doubleClick(element).perform();
System.out.println("Double click performed");doubleClick() will execute without exceptions.Actions action = new Actions(driver); action.doubleClick(); action.perform();
doubleClick() method requires a target WebElement to act on. Calling it without an element is incorrect.perform() after building the action is correct. The method exists and Actions object need not be static.submitBtn and then verify if a confirmation message with id confirmMsg appears. Which of the following code snippets correctly performs this and asserts the message is displayed?new Actions(driver).doubleClick(btn).perform(); to double click it.assertTrue(msg.isDisplayed());.Pass the test and this pattern moves unknown