我们如何将 ExpectedConditions 作为参数传递给 Selenium Java 中的 wait.until?
How can we pass in an ExpectedConditions as a parameter to wait.until in Selenium Java?
我发现将 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 等可以传入的地方调用的方法。
谢谢。
WebDriverWait 有一个构造函数,它被重载为需要很长时间作为第二个参数。您不需要使用 Duration.ofSeconds(seconds)
此外,
The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedConditions)
修复:
您应该将 expectedConditions 与一个连词一起使用,例如
- elementToBeClickable
- visibilityOfElement
等等..
代码:
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);
}
我没有发现您的代码块有任何问题。但是,您需要确保以下几点:
在使用 ExpectedConditions 时,您必须进行以下导入:
import org.openqa.selenium.support.ui.ExpectedConditions;
因为你正在使用 Selenium 4.0.0 你需要使用 guava-31.0.1-jre.jar
(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions)
首先,expectedConditions
类型错误
您将其声明为ExpectedConditions
,代表实用程序class。
您实际上想要 ExpectedCondition
,ExpectedConditions
returns.
中所有方法的类型
但仅仅将其更改为 ExpectedCondition
是不够的。因为你会收到关于 Raw type
的警告,因为 ExpectedCondition
是通用的 class.
所以要声明class的类型参数,因为要包含所有,所以使用通配符?
最后,参数应该是ExpectedCondition<?> expectedConditions
我发现将 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 等可以传入的地方调用的方法。
谢谢。
WebDriverWait 有一个构造函数,它被重载为需要很长时间作为第二个参数。您不需要使用 Duration.ofSeconds(seconds)
此外,
The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedConditions)
修复:
您应该将 expectedConditions 与一个连词一起使用,例如
- elementToBeClickable
- visibilityOfElement
等等..
代码:
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);
}
我没有发现您的代码块有任何问题。但是,您需要确保以下几点:
在使用 ExpectedConditions 时,您必须进行以下导入:
import org.openqa.selenium.support.ui.ExpectedConditions;
因为你正在使用 Selenium 4.0.0 你需要使用
guava-31.0.1-jre.jar
(WebElement element, long seconds, String errorMessage, ExpectedConditions expectedConditions)
首先,expectedConditions
类型错误
您将其声明为ExpectedConditions
,代表实用程序class。
您实际上想要 ExpectedCondition
,ExpectedConditions
returns.
但仅仅将其更改为 ExpectedCondition
是不够的。因为你会收到关于 Raw type
的警告,因为 ExpectedCondition
是通用的 class.
所以要声明class的类型参数,因为要包含所有,所以使用通配符?
最后,参数应该是ExpectedCondition<?> expectedConditions