4

I'm trying to test a simple drag and drop behavior on a React app.

I am using the react-dnd-treeview library and their example website to test my test case.

When I run the tests in debug, I don't get any errors and Selenium is able to get the right elements, but nothing is happening, I'm not able to create or visualize any sort of actions, even after trying so many of the different answers in this similar question, but in vain.

Here is the code I am working with:

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.io.File;

public class DAndDJava {

    public static void main(String[] args) {

        File file = new File("C:/misc/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver" , file.getAbsolutePath());

        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://teleport.github.io/react-dnd-treeview/example/dist/index.html");

        WebElement dragPoint = driver.findElement(By.xpath ("//*[@id=\"root\"]/div/div/div[3]/div[2]/div[2]/div/div/div[3]/div[2]/div/div[1]/div[3]/div[1]/div"));
        WebElement dropPoint = driver.findElement(By.xpath ("//*[@id=\"root\"]/div/div/div[3]/div[2]/div[2]/div/div/div[3]/div[2]/div/div[1]/div[3]/div[1]"));

        Actions builder = new Actions(driver);

        Action dragAndDrop = builder.clickAndHold(dragPoint)
                                    .moveToElement(dropPoint)
                                    .release(dropPoint)
                                    .build();

        dragAndDrop.perform();

        driver.quit();
    }

}
Antoine
  • 800
  • 3
  • 14
  • 29

2 Answers2

0

Could you try with below code:

Action dragAndDrop = builder.clickAndHold(dragPoint)
                            .moveToElement(dropPoint)
                            .moveByOffset(0,10)   
                            .release()
                            .build()
                            .perform() ;
Prany
  • 2,078
  • 2
  • 13
  • 31
-1

Solution is described here

First cursor movement should be within element borders in order to change the state of the element to dragged, and only then you can drag it to the expected position:

action.clickAndHold(elementToDrag)
    .moveByOffset(0, -5)
    .pause(100)
    .moveByOffset(0, -300)
    .release()
    .perform();
myeongkil kim
  • 2,465
  • 4
  • 16
  • 22