reactjs - 如何使用 Java 中的 Selenium 测试 react-beautiful-dnd 库中的“拖放”功能?
问题描述
我尝试了各种选项,但无法在使用 Selenium 的浏览器中模拟鼠标单击以将元素从一个位置拖动到另一个位置。测试运行时,我看到元素被选中,但它没有移动到指定的放置点。非常感谢任何建议或见解!
这是我在最近一次尝试中定义函数的方式(这个主题的变体也尝试过但失败了):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
builder.clickAndHold(dragPoint).perform();
builder.pause(Duration.ofSeconds(1));
builder.moveByOffset(10,0).perform();
builder.moveToElement(dropPoint).perform();
builder.moveByOffset(10,0).perform();
builder.pause(Duration.ofSeconds(1));
builder.release();
builder.build();
builder.perform();
}
还尝试了以下(相同的结果):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(dragPoint, dropPoint).build();
dragAndDrop.perform();
}
在测试中,使用 xpath 唯一标识 2 个元素并调用该函数:
WebElement dragPoint = driver.findElement(By.xpath(".../div[3]/...(etc.)/div[@class='rst__moveHandle']"));
WebElement dropPoint = driver.findElement(By.xpath(".../div[5]/...(etc.)/div[@class='rst__moveHandle']"));
dragAndDrop(dragPoint, dropPoint, driver);
相关库:
react-beautiful-dnd: https://github.com/atlassian/react-beautiful-dnd
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
解决方案
推荐阅读
- php - 从复选框 laravel 更改用户状态
- arrays - 找到最小的最常见值,如果所有常见值都相同,则找到第一个零或下一个最小的数字
- c++ - 具有条件的 3 个嵌套循环的时间复杂度
- postgresql - 如何使用 postgres 从站创建 jenkins-master
- c++ - 使阶乘程序更快?
- java - IText 7 未知密钥算法:ECGOST3410
- swift - 如何将 DispatchQueue debounce 转换为 Swift 并发任务?
- regex - 如何用正则表达式替换自身的 n 个重复项?
- javascript - 三元操作在reactJS中的按钮单击上不起作用?
- javascript - 如何将有关上传文件的数据添加到对象数组并在其他组件中使用数据?在 VUE 上