有人能解释清楚什么是ExpectedCondition布尔泛型类型吗?
new WebDriverWait(driver, 60).until((ExpectedCondition<Boolean>) wd->((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));发布于 2018-11-14 04:05:11
Boolean是lamda表达式的返回类型。在您的示例中,javascript .equals("complete")在javascript executor中的最后一行返回布尔值。
例如,下面的示例返回WebElement,
new WebDriverWait(driver, 60).until((ExpectedCondition<WebElement>) wd->((JavascriptExecutor) wd).executeScript("return document.getElementById(someid)"));
new WebDriverWait(driver, 60).until((ExpectedCondition<WebElement>) wd-> wd.findElement(By.id("someid")););发布于 2018-11-14 22:25:06
在ExpectedCondition的代码中,我们可以看到:
public interface ExpectedCondition<T> extends Function<WebDriver, T> 可以理解为"Expected is always接受WebDriver作为参数并返回泛型类型T>的值的函数>
在Function的代码中,我们可以看到:
/**
* @param `<T>` the type of the input to the function
* @param `<R>` the type of the result of the function
*/
public interface Function<T, R> 您的函数比较两个对象:
1) executeScript("return document.readyState")返回的Object。实际上,js document.readyState返回一个装入/ String /complete的互动值。more info here
2) String“完成”
使用返回boolean值的方法.equals():
((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete")所以你的函数的返回值是一个boolean,它应该作为返回值类型传递,但是java中的泛型不允许原语,所以你必须使用Boolean。
Java中的泛型是编译时构造-编译器将所有泛型的用法转换为正确的类型。任何用作泛型的东西都必须可以转换为Object,而基元类型则不能。
https://stackoverflow.com/questions/53288217
复制相似问题