Selenium WebDriver implements Selenium RC's dragAndDrop
command using the Actions
class. As seen in earlier recipes, the Actions
class supports advanced user interactions such as firing various mouse and keyboard events. We can build simple or complex chains of events using this class.
In this recipe, we will use the Actions
class to perform drag-and-drop operations.
Let's implement a test that will perform a drag-and-drop operation on a page using the Actions
class:
@Test public void testDragDrop() { driver.get("http://cookbook.seleniumacademy.com/DragDropDemo.html"); WebElement source = driver.findElement(By.id("draggable")); WebElement target = driver.findElement(By.id("droppable")); Actions builder = new Actions(driver); builder.dragAndDrop(source, target) .perform(); assertEquals("Dropped!", target.getText()); }