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 |