【问题标题】:How can we pass in an ExpectedConditions as a parameter to wait.until in Selenium Java?我们如何在 Selenium Java 中将 ExpectedConditions 作为参数传递给 wait.until?
【发布时间】:2021-12-19 15:07:23
【问题描述】:

我发现将ExpectedConditions 作为方法中的参数传递给wait.until() 时遇到问题。 wait.until() 期望传入一个函数。我对 Java 比较陌生,希望能得到帮助。

违规代码:

public void waitUntil(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions) {
 if (seconds == 0 || errorMessage.isEmpty()) throw new IllegalArgumentException();
 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(seconds));
 wait.withMessage(errorMessage);
 // this throws a compiler error.
 wait.until(expectedConditions);
}

wait.until() 期望传入一个函数,类似于 ExpectedConditions.urlToBe("http://www.test.com")

我正在尝试创建一个可以在任何 ExpectedCondition (即 urlToBe、alertIsPresent 等)可以被传入的地方调用的方法。

谢谢。

【问题讨论】:

  • 编译器错误说明了什么?
  • Required type: Function <? super org.openqa.selenium.WebDriver, V> Provided: ExpectedConditions reason: no instance(s) of type variable(s) V exist so that ExpectedConditions conforms to Function<? super WebDriver, V>
  • 如果把第4个参数改成:ExpectedCondition<?> expectedConditions呢?
  • > 应该是什么类型?
  • 只需将其保留为 ? 即可。它被称为通配符:docs.oracle.com/javase/tutorial/java/generics/wildcards.html

标签: java selenium webdriver


【解决方案1】:
(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions)

首先,expectedConditions 的类型是错误的。

您将其声明为ExpectedConditions,它代表 util 类。

您实际上想要ExpectedConditionExpectedConditions 中的所有方法都返回的类型。

但仅将其更改为 ExpectedCondition 是不够的。因为你会收到关于Raw type 的警告,因为ExpectedCondition 是一个泛型类。

所以你必须声明类的类型参数,并且因为你想包含所有内容,所以你使用通配符?

最后,参数应该是ExpectedCondition<?> expectedConditions

【讨论】:

  • 完美运行,再次感谢您。
【解决方案2】:

WebDriverWait 有一个构造函数,该构造函数被重载以花费第二个参数。你不需要使用Duration.ofSeconds(seconds)

还有,

The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedConditions)

修复:

您应该将 expectedConditions 与类似的连词一起使用

  1. elementToBeClickable
  2. 元素的可见性

等等..

代码:

public void waitUntil(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions) {
     if (seconds == 0 || errorMessage.isEmpty()) throw new IllegalArgumentException();
     WebDriverWait wait = new WebDriverWait(driver, seconds);
     wait.withMessage(errorMessage);
     WebElement elemenet  = wait.until(expectedConditions.elementToBeClickable(By.xpath("//some xpath")));
    }

内部重载方法:

  */
  public WebDriverWait(WebDriver driver, long timeOutInSeconds) {
    this(
        driver,
        java.time.Clock.systemDefaultZone(),
        Sleeper.SYSTEM_SLEEPER,
        timeOutInSeconds,
        DEFAULT_SLEEP_TIMEOUT);
  }

【讨论】:

  • 问题是,现在只使用longint 已被弃用,它上面有@Deprecated 注释。 Selenium 作者希望我们使用 Duration.ofSeconds() 作为参数。
  • @SilentKay : 你用的是哪个版本?
  • 硒 4.0.0。只需添加 -> 我知道您可以使用 ExpectedConditions.urlToBe(url) 并将 url 传递给它。但这意味着我必须为每个条件创建一个单独的方法。然而,我宁愿它是通用的,所以我可以将 ExpectedCondition 对象传递给方法,调用者认为合适的任何条件。
【解决方案3】:

我在您的代码块中没有发现任何错误。但是,您需要确保以下几点:

  • 在使用 ExpectedConditions 时,您必须进行以下导入:

    import org.openqa.selenium.support.ui.ExpectedConditions;
    
  • 当您使用 Selenium 4.0.0 时,您需要使用 guava-31.0.1-jre.jar

【讨论】:

  • 是的,为了简洁起见,我删除了它。但它已经存在了。
  • 交叉检查番石榴版本。
  • 我不确定“番石榴”与将静态方法传递给要调用的方法有什么关系?
  • 看来你有一个工作和一个接受的答案。所以我将跳过解释。祝你好运
猜你喜欢
  • 2017-07-14
  • 1970-01-01
  • 2014-03-12
  • 2021-09-09
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
相关资源
最近更新 更多