如何在 react-dnd-treeview 库上使用 Selenium 测试拖放



我正在尝试在 React 应用程序上测试简单的拖放行为。

我正在使用 react-dnd-treeview 库和他们的示例网站来测试我的测试用例。

当我在调试中运行测试时,我没有收到任何错误,Selenium 能够获得正确的元素,但什么也没发生,我无法创建或可视化任何类型的操作,即使在这个类似的问题中尝试了这么多不同的答案,但徒劳无功。

这是我正在使用的代码:

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

你能试试下面的代码吗:

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

此处描述了解决方案

  • 反应-美丽-DND/问题

第一次光标移动应该在元素边框内,以便将元素的状态更改为dragged,然后才能将其拖动到预期位置:

action.clickAndHold(elementToDrag)
.moveByOffset(0, -5)
.pause(100)
.moveByOffset(0, -300)
.release()
.perform();

最新更新