Test Overview
This test automates dragging an element and dropping it onto a target area on a web page. It verifies that the drop action was successful by checking the target's text change.
This test automates dragging an element and dropping it onto a target area on a web page. It verifies that the drop action was successful by checking the target's text change.
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 DragAndDropTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://example.com/drag_and_drop"); } @Test public void testDragAndDrop() { WebElement source = driver.findElement(By.id("draggable")); WebElement target = driver.findElement(By.id("droppable")); Actions actions = new Actions(driver); actions.dragAndDrop(source, target).perform(); String targetText = target.getText(); assertEquals("Dropped!", targetText, "The drop target text should be 'Dropped!'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is maximized and ready | — | PASS |
| 2 | Navigates to https://example.com/drag_and_drop | Page with draggable and droppable elements is loaded | — | PASS |
| 3 | Finds draggable element by id 'draggable' | Draggable element is located on the page | — | PASS |
| 4 | Finds droppable element by id 'droppable' | Droppable element is located on the page | — | PASS |
| 5 | Performs dragAndDrop action from draggable to droppable | Draggable element is moved and dropped onto droppable element | — | PASS |
| 6 | Gets text of droppable element after drop | Droppable element text is retrieved | Verify droppable element text equals 'Dropped!' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
dragAndDrop method in Selenium's Actions class?dragAndDrop method is designed to simulate the user action of clicking and holding an element, moving it to another element, and releasing it.dragAndDrop(source, target) followed by perform() to execute the action.WebElement source = driver.findElement(By.id("item1"));
WebElement target = driver.findElement(By.id("box1"));
Actions actions = new Actions(driver);
actions.clickAndHold(source).moveToElement(target).release().perform();
String text = target.getText();text contain?source onto target usually moves or copies the source element's content into the target area.target.getText() returns after dropgetText() method returns the visible text of the target element, which typically does not change automatically after drag and drop.WebElement source = driver.findElement(By.id("drag"));
WebElement target = driver.findElement(By.id("drop"));
Actions actions = new Actions(driver);
actions.clickAndHold(source).moveToElement(target).perform();release(), so the drop action is incomplete and will not work as expected.release() before perform() -> Option APass the test and this pattern moves unknown