Test Overview
This test opens a webpage, finds a button, clicks and holds it, then verifies the button's text changes to show it is held.
This test opens a webpage, finds a button, clicks and holds it, then verifies the button's text changes to show it is held.
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.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class ClickAndHoldTest { WebDriver driver; Actions actions; @BeforeEach public void setUp() { driver = new ChromeDriver(); actions = new Actions(driver); } @Test public void testClickAndHoldButton() { driver.get("https://example.com/click-and-hold"); WebElement button = driver.findElement(By.id("hold-button")); actions.clickAndHold(button).perform(); String buttonText = button.getText(); assertEquals("Holding", buttonText); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test environment initialized, ChromeDriver ready | — | PASS |
| 2 | Browser opens Chrome | Chrome browser window opened, blank page | — | PASS |
| 3 | Navigates to https://example.com/click-and-hold | Page with a button having id 'hold-button' loaded | — | PASS |
| 4 | Finds element by id 'hold-button' | Button element located on the page | Element is present and interactable | PASS |
| 5 | Clicks and holds the button using Actions.clickAndHold().perform() | Button visually pressed and held down | — | PASS |
| 6 | Gets button text after click and hold | Button text changed to 'Holding' | Button text equals 'Holding' | PASS |
| 7 | Test ends and browser closes | Browser closed, resources released | — | PASS |
clickAndHold() method in Selenium's Actions class do?clickAndHold() method simulates pressing and holding the mouse button down on a specific web element without releasing it.clickAndHold() keeps the mouse button pressed, useful for drag-and-drop or selection actions.element using Selenium in Java?new Actions(driver) to perform complex mouse actions.clickAndHold(element) followed by perform() to execute the action.Actions actions = new Actions(driver);
WebElement box = driver.findElement(By.id("box"));
actions.clickAndHold(box).pause(Duration.ofSeconds(2)).release().perform();clickAndHold(box) to press the mouse button down on the element, then pauses for 2 seconds, then releases the mouse button.pause(Duration.ofSeconds(2)) waits 2 seconds while holding, and release() releases the mouse button, then perform() executes all actions.Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("drag"));
new Actions(driver).clickAndHold(element);
actions.perform();clickAndHold(element) returns an Actions object and should be chained with perform() to execute immediately.clickAndHold(element) is called but not chained; then actions.perform() is called separately, which performs no action because the previous call was not chained.source, move it to an element with id target, then release. Which of the following code snippets correctly performs this using Selenium Java?clickAndHold(source), moveToElement(target), release(), and perform(). new Actions(driver).click(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).click().perform(); uses click instead of clickAndHold, new Actions(driver).dragAndDrop(By.id("source"), By.id("target")).perform(); incorrectly passes By locators instead of WebElements, new Actions(driver).clickAndHold(By.id("source")).moveToElement(By.id("target")).release().perform(); incorrectly passes By locators instead of WebElements.Pass the test and this pattern moves unknown