First WebDriver/Selenium DragAndDrop does not appear to work anymore, at best it goes between working and not working depending on release of drivers, browsers and selenium. I do not find that any of the java workarounds actually work.
clickAndHold() moveToElement() release() // solutions don't work
I put together a working Java example after viewing the below mentioned sites. My solution is using rcorreia's javascript
Elemental Selenium Drag N Drop - example app for testing Drag N Drop
rcorreia/drag_and_drop_helper.js - rcorreia's javascript helper code
RomanIsko/elemental-selenium-tips - a csharp implementation
I figured out how to integrate the javascript file by looking at romanisko csharp code.
JSDriver.ExecuteScript(dnd_javascript + "$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");
From the above I came up with the following Java Solution:
DragAndDropJsHelper.java
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class DragAndDropJsHelper {
Logger log = LogManager.getLogger(DragAndDropJsHelper.class.getName());
String dragndrop_js = null;
public DragAndDropJsHelper(String jsFile) throws IOException {
// the javascript file must be read into a single string no line breaks
try (BufferedReader br = Files.newBufferedReader(Paths.get(getURIFromURL(jsFile)))) {
dragndrop_js = br
.lines()
.collect(Collectors.joining(" "));
} catch (IOException e) {
log.error(e.getMessage());
}
}
// helper method, I'm using maven my js file is in src/test/resources
@SuppressWarnings("finally")
private URI getURIFromURL(String fileName) {
URI uri = null;
try {
URL url = this.getClass().getClassLoader().getResource(fileName);
uri = url.toURI();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
return uri;
}
}
/**
* Solution from elemental-selenium-tips
* https://github.com/tourdedave/elemental-selenium-tips/blob/master/39-drag-and-drop/csharp/DragAndDrop.cs
* JSDriver.ExecuteScript(dnd_javascript + "$('#column-a').simulateDragDrop({
* dropTarget: '#column-b'});");
*
* NOTE: Seems fragile, not sure if this works for XPATH or other src/dst type strings
* TODO: would be good if it worked with WebElement, or BY
*
* @param driver
* @param src
* - css string for source element
* @param dst
* - css string for destination element
*/
public void dragDrop(WebDriver driver, String src, String dst) {
String js = String.format("$('%s').simulateDragDrop({ dropTarget: '%s'});",src,dst);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(dragndrop_js + js);
}
}
Part of my DragAndDropTest.java
@Test
public void dragAtoBTest() {
try {
WebElement weA = driver.findElement(DragNDropPage.COLUMN_A);
WebElement weB = driver.findElement(DragNDropPage.COLUMN_B);
DragAndDropJsHelper ddh = new DragAndDropJsHelper("drag_and_drop_helper.js");
ddh.dragDrop(driver, "#column-a", "#column-b");
log.info("wait for opacity to change 1, indicating move complete");
wait.until(ExpectedConditions.attributeToBe(weA, "opacity", "1"));
assertThat(weA.findElement(page.columnHeader).getText(), containsString("B"));
} catch (Exception e) {
e.printStackTrace();
log.error(e);
Assert.fail("FAILED validLoginTest");
}
}
NOTE: Using the drag_and_drop.js requires the src and dst to be css selector strings, I don't know if xpath would work. It would be nicer if
it would handle WebElement and/or BY for input parameters. Unfortunately, my javascript is not deep enough to make that modification.
see also:
Selenium Actions drag and drop not working on chrome browser
Drag and drop support for an object via Gwen DSL