0
0
Selenium Javatesting~10 mins

Drag and drop in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium WebDriver
Selenium Java
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();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is maximized and ready-PASS
2Navigates to https://example.com/drag_and_dropPage with draggable and droppable elements is loaded-PASS
3Finds draggable element by id 'draggable'Draggable element is located on the page-PASS
4Finds droppable element by id 'droppable'Droppable element is located on the page-PASS
5Performs dragAndDrop action from draggable to droppableDraggable element is moved and dropped onto droppable element-PASS
6Gets text of droppable element after dropDroppable element text is retrievedVerify droppable element text equals 'Dropped!'PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The draggable or droppable element is not found or the drop action does not change the target text
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to perform the drag and drop?
Adriver.dragAndDrop(source, target)
Bactions.dragAndDrop(source, target).perform()
Csource.dragTo(target)
Dactions.clickAndHold(source).release(target)
Key Result
Use explicit and reliable locators for draggable and droppable elements and verify the expected UI change after drag and drop to ensure the test validates the actual user interaction.