Performing drag-and-drop operations
Selenium WebDriver implements Selenium RC's dragAndDrop
command using 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.
How to do it...
Let's implement a test which will perform a drag-and-drop operation on a page using the Actions
class.
@Test public void testDragDrop() { driver.get("http://dl.dropbox.com/u/55228056/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(); try { assertEquals("Dropped!", target.getText()); } catch (Error e) { verificationErrors.append(e.toString()); } }
How it works...
For dragging an element...