How to test drag and drop with Selenium on the react-dnd-treeview library

筅森魡賤 提交于 2020-01-13 05:33:06

问题


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();
    }

}

回答1:


Could you try with below code:

Action dragAndDrop = builder.clickAndHold(dragPoint)
                            .moveToElement(dropPoint)
                            .moveByOffset(0,10)   
                            .release()
                            .build()
                            .perform() ;


来源:https://stackoverflow.com/questions/50295160/how-to-test-drag-and-drop-with-selenium-on-the-react-dnd-treeview-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!