如何在Java中使用Selenium从react-beautiful-dnd库测试“拖放”功能?

时间:2019-02-03 17:59:42

标签: reactjs selenium selenium-webdriver automated-tests 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唯一标识了这两个元素,该函数称为:

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;

0 个答案:

没有答案