Selenium (Java): dndScript() 方法未定义

Selenium (Java): the method dndScript() is undefined

我正在尝试验证一位前同事的旧演示 Selenium 脚本是否仍然有效,但是 运行 出现了一个奇怪的错误。不幸的是,同事不再在身边咨询。首先,这是有问题的脚本,它应该将一个元素拖放到页面上的另一个元素上:

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class DragNDrop {
    
    @Test
    public void testDragAndDropWithCheck() throws InterruptedException {
        
        System.setProperty("webdriver.gecko.driver","D:\WebDriver\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();

        driver.get("https://the-internet.herokuapp.com/drag_and_drop");

        By css = By.cssSelector("div[id^=\"column-\"]");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        
        Supplier<List<WebElement>> fetchComponents = () -> wait
                .until(ExpectedConditions.presenceOfAllElementsLocatedBy(css));
        /**
         * Starting check for element position
         */
        List<WebElement> startingCheck = fetchComponents.get();

        Assert.assertEquals("Starting - Draggable number does not match!", 2, startingCheck.size());
        Assert.assertEquals("Starting - A position does not match!", "A", startingCheck.get(0).getText());
        Assert.assertEquals("Starting - B position does not match!", "B", startingCheck.get(1).getText());

        int index = ThreadLocalRandom.current().nextInt(startingCheck.size());
        WebElement from = startingCheck.get(index);
        WebElement to = startingCheck.get(1 - index);

        JavascriptExecutor jse = (JavascriptExecutor) driver;

        jse.executeScript(
            dndScript() + "simulateDragAndDrop(arguments[0], arguments[1])",
            from,
            to);
        /**
         * Ending check for element position
         */
        List<WebElement> endingCheck = fetchComponents.get();

        Assert.assertEquals("Ending - Draggable number does not match!", 2, endingCheck.size());
        Assert.assertEquals("Ending - A position does not match!", "A", endingCheck.get(1).getText());
        Assert.assertEquals("Ending - B position does not match!", "B", endingCheck.get(0).getText());
    }

}

这部分有问题:

jse.executeScript(
    dndScript() + "simulateDragAndDrop(arguments[0], arguments[1])",
    from,
    to);

Eclipse 突出显示 dndScript() 并抛出消息:

The method dndScript() is undefined for the type DragNDrop

我首先认为这是由于缺少导入,所以我开始谷歌搜索,但找不到任何相关信息。我能找到的最多的是一些对名为“RichFaces”的东西的引用,但我找不到任何进一步的说明(可能是我自己的错——我绝不是 Selenium/Java 专家)。

知道这个函数的故事是什么以及如何在此脚本中正确实现它吗?

这不太可能与丢失的导入(可能是静态导入)有关。由于这只是一个方法名称,因此可能存在三种情况:

  1. 这是一些不同class范围内的静态方法,过去使用静态导入
  2. 导入
  3. 这是您当前class
  4. 范围内必须实施的方法
  5. 这是必须在父级范围内实现的方法 classes

您目前拥有的原因是您当前 class 的代码从那时起发生了变化。如果您从版本控制中获取此代码,则需要检查修订历史。

有几个可能的故事似乎是可能的:

  • 当前 class 范围内有一个方法,然后它被移动到某个父级 class。由于某些原因,有人忘记将 extends 关键字添加到当前 class.
  • 在父 class 范围内有一个方法。在那之后有人决定打破 classes 之间的关系并忘记将该方法移动到您当前的 class.
  • 在一些不同的 class 中有静态方法。它作为 static import 导入到当前 class。然后有人将 class 加载到 IDE 并且没有将“不同的 class” 添加到项目的 class 路径。然后他们应用了“组织导入”功能,该功能从您当前的 class.
  • 中删除了损坏的导入