In the end, I had to use a Java robot to make it work. Not only in order to see the mouse, but also because for the HTML5 web application, the drag and drop is split into selenium, because for the drag and drop, two movements are required for registration. Selenium does only one.
My method is dragging from the center of each object and allows offset if you want to drag the item you are dragging.
public void dragAndDropElement(WebElement dragFrom, WebElement dragTo, int xOffset) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(2000);
Dimension fromSize = dragFrom.getSize();
Dimension toSize = dragTo.getSize();
int xCentreFrom = fromSize.width / 2;
int yCentreFrom = fromSize.height / 2;
int xCentreTo = toSize.width / 2;
int yCentreTo = toSize.height / 2;
Point toLocation = dragTo.getLocation();
Point fromLocation = dragFrom.getLocation();
toLocation.x += xOffset + xCentreTo;
toLocation.y += yCentreTo;
fromLocation.x += xCentreFrom;
fromLocation.y += yCentreFrom;
robot.mouseMove(fromLocation.x, fromLocation.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(((toLocation.x - fromLocation.x) / 2) + fromLocation.x, ((toLocation.y - fromLocation.y) / 2) + fromLocation.y);
robot.mouseMove(toLocation.x, toLocation.y);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
source
share