0
0
Selenium Javatesting~15 mins

Action chain execution (perform) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Perform drag and drop using action chains
Preconditions (3)
Step 1: Locate the draggable element by id 'draggable'
Step 2: Locate the droppable element by id 'droppable'
Step 3: Use action chains to click and hold the draggable element
Step 4: Move the draggable element to the droppable element
Step 5: Release the mouse button to drop the element
Step 6: Perform the action chain
Step 7: Verify that the droppable element's text changes to 'Dropped!'
✅ Expected Result: The draggable element is dropped onto the droppable element and the droppable element's text changes to 'Dropped!'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the droppable element's text is 'Dropped!' after the action chain is performed
Best Practices:
Use explicit waits to ensure elements are visible and interactable before actions
Use Actions class for complex user gestures
Use meaningful locators (By.id) for elements
Perform the action chain with perform() method
Use try-catch or test framework assertions for validation
Automated Solution
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.Assert;
import java.time.Duration;

public class DragAndDropTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("https://example.com/drag_and_drop_page");

            // Wait for draggable element
            WebElement draggable = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("draggable")));
            // Wait for droppable element
            WebElement droppable = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("droppable")));

            Actions actions = new Actions(driver);
            actions.clickAndHold(draggable)
                   .moveToElement(droppable)
                   .release()
                   .perform();

            // Verify the droppable text changed to 'Dropped!'
            String dropText = droppable.getText();
            Assert.assertEquals("Dropped!", dropText);

            System.out.println("Test Passed: Drag and drop performed successfully.");
        } catch (Exception e) {
            System.out.println("Test Failed: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate a drag and drop action.

First, it opens the browser and navigates to the test page.

It waits explicitly for the draggable and droppable elements to be visible using WebDriverWait and ExpectedConditions.

Then, it creates an Actions object to build the action chain: click and hold the draggable element, move it to the droppable element, and release the mouse button.

The perform() method executes the chain.

Finally, it asserts that the droppable element's text changed to 'Dropped!' to confirm the action succeeded.

The test prints a pass or fail message and closes the browser.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
{'mistake': 'Not calling perform() after building the action chain', 'why_bad': "Without perform(), the actions are not executed, so the drag and drop won't happen.", 'correct_approach': 'Always call perform() at the end of the action chain to execute it.'}
Using brittle locators like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different draggable and droppable element pairs on the page

Show Hint