Test Overview
This test demonstrates why complex gestures like drag-and-drop require Selenium's Actions API. It verifies that a drag-and-drop action moves an element to the target location successfully.
This test demonstrates why complex gestures like drag-and-drop require Selenium's Actions API. It verifies that a drag-and-drop action moves an element to the target location 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.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.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, "Drag and drop failed to update target text."); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser is open with blank page | - | PASS |
| 2 | Navigates to https://example.com/drag_and_drop | Page with draggable and droppable elements is loaded | - | PASS |
| 3 | Finds element with id 'draggable' as source | Source element is located on the page | - | PASS |
| 4 | Finds element with id 'droppable' as target | Target element is located on the page | - | PASS |
| 5 | Creates Actions object and performs dragAndDrop from source to target | Drag-and-drop gesture is executed on the page | - | PASS |
| 6 | Gets text from target element after drop | Target element text is retrieved | Verify target text equals 'Dropped!' | PASS |
| 7 | Test ends and browser closes | Browser is closed | - | PASS |