There are several web applications that provides services for drag an element and drop on certain element on browser.
Some examples:
Can customize menu bar items by drag child elements on parent element.
Drag and drop files/folders in some hierarchy manner.
There are several ways we can automate drag and drop, any one of the following methods can be used:
-click and hold on the child element and release the element on parent element.
action.clickAndHold(child).moveToElement(parent).release(child).build().perform();
-drag an element and drop on parent element.
action.dragAndDrop(child, parent).build().perform();
-click and hold the child element and drop on a particular co-ordinate on web application
Point p = parent.getLocation();
int px=p.getX();
int py=p.getY();
action.dragAndDropBy(child, px, py).build().perform();
Let’s see the full code implementation:
public class DragnDrop {
public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://dhtmlx.com/docs/products/dhtmlxTree/");
System.out.println(driver.getTitle());
Thread.sleep(5000);
WebElement parent = driver.findElement(By.xpath("//div[@id='treebox2']//span[text()='Ian Rankin']"));
WebElement child = driver.findElement(By.xpath("//div[@id='treebox2']//span[text()='James Johns']"));
Actions action = new Actions(driver);
//action.clickAndHold(child).moveToElement(parent).release(child).build().perform();
action.dragAndDrop(child, parent).build().perform();
System.out.println("Done");
}
}
one more site to practice drag and drop:
http://html5demos.com/drag
https://www.dartlang.org/samples/dnd/