I need to perform a Drag and Drop using Selenium and Java. I used dragAndDrop() method in Action class for this. but it didn't work. From element is selected but its not moving to the To element. I checked the Xpath of the To element. No problem with the Xpath.
This is the screen that I try to automate.
These are the codes that I tried
public void dragAndDropUsers(String username) throws InterruptedException {
//Actions class method to drag and drop
Actions builder = new Actions(WebDriverHelper.getWebDriver());
Thread.sleep(2000);
WebElement from = WebDriverHelper.getWebDriver().findElement(By.xpath("//span[contains(text(),'"+username+"')]"));
WebElement to = WebDriverHelper.getWebDriver().findElement(By.xpath("//h6[text()='Sequential Approval Order']/following-sibling::p-tree"));
builder.dragAndDrop(from,to).build().perform();
}
above code selecting the To element. But not move.
public void dragAndDropUsers(String username) throws InterruptedException {
//Actions class method to drag and drop
Actions builder = new Actions(WebDriverHelper.getWebDriver());
Thread.sleep(2000);
WebElement from = WebDriverHelper.getWebDriver().findElement(By.xpath("//span[contains(text(),'"+username+"')]"));
WebElement to = WebDriverHelper.getWebDriver().findElement(By.xpath("//h6[text()='Sequential Approval Order']/following-sibling::p-tree"));
builder.clickAndHold(from).moveToElement(to).release().perform();
Thread.sleep(5000);
}
same result for above code. (selecting the To element. But not move. )
public void dragAndDropUsers(String username) throws InterruptedException {
//Actions class method to drag and drop
Actions builder = new Actions(WebDriverHelper.getWebDriver());
Thread.sleep(2000);
WebElement from = WebDriverHelper.getWebDriver().findElement(By.xpath("//span[contains(text(),'"+username+"')]"));
WebElement to = WebDriverHelper.getWebDriver().findElement(By.xpath("//h6[text()='Sequential Approval Order']/following-sibling::p-tree"));
JavascriptExecutor _js = (JavascriptExecutor) WebDriverHelper.getWebDriver();
_js.executeScript("$(arguments[0]).simulate('drag-n-drop',{dragTarget:arguments[1],interpolation:{stepWidth:100,stepDelay:50}});", from, to);
Thread.sleep(5000);
}
Above code displayed an error as "org.openqa.selenium.JavascriptException: javascript error: $(...).simulate is not a function"
public void dragAndDropUsers(String username) throws InterruptedException {
//Actions class method to drag and drop
Actions builder = new Actions(WebDriverHelper.getWebDriver());
Thread.sleep(2000);
WebElement from = WebDriverHelper.getWebDriver().findElement(By.xpath("//span[contains(text(),'"+username+"')]"));
WebElement to = WebDriverHelper.getWebDriver().findElement(By.xpath("//h6[text()='Sequential Approval Order']/following-sibling::p-tree"));
int x = to.getLocation().getX();
int y = to.getLocation().getY();
System.out.println("x coordinate: "+x);
System.out.println("y coordinate: "+y);
builder.moveToElement(from)
.pause(Duration.ofSeconds(1))
.clickAndHold(from)
.pause(Duration.ofSeconds(1))
.moveByOffset(x, y)
.moveToElement(to)
.pause(Duration.ofSeconds(1))
.release().build().perform();
Thread.sleep(5000);
}
Above code is also selecting the From element. But not moving.
Help me to resolve this issue.