英文:
How to make a method return type as Callable<Boolean>
问题
在我的一个方法中:
public void pageIsReady()
实现是:
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(isPageLoaded());
这里,isPageLoaded()
方法返回一个布尔值,但我希望它返回一个 Callable
的布尔值,因为 Awaitility
中的 until()
方法需要一个 Callable<Boolean>
。
请帮助我将 isPageLoaded()
方法修改为返回 Callable<Boolean>
。
以下是 isPageLoaded()
方法的实现:
protected Callable<Boolean> isPageLoaded() {
String jsQuery = "function pageLoaded() "
+ "{var loadingStatus=(document.readyState=='complete');"
+ "return loadingStatus;};"
+ "return pageLoaded()";
return () -> {
try {
Boolean isSuccess = (Boolean) evaluateJavascript(jsQuery);
return isSuccess;
} catch (Exception exp) {
exp.printStackTrace();
return false;
}
};
}
英文:
In one of my methods:
public void pageIsReady()
the implementation is
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(isPageLoaded());
Here, isPageLoaded()
method returns boolean value, but I want it to return a Callable
of Boolean, because the until()
method in Awaitility
expects Callable<Boolean>
.
Please help me to make the method isPageLoaded()
return Callable<Boolean>
Here is the implementation of isPageLoaded()
method:
protected Boolean isPageLoaded() {
String jsQuery = "function pageLoaded() "
+ "{var loadingStatus=(document.readyState=='complete');"
+ "return loadingStatus;};"
+ "return pageLoaded()";
boolean isSuccess = false;
try {
isSuccess = (Boolean) evaluateJavascript(jsQuery);
} catch (Exception exp) {
exp.printStackTrace();
}
return isSuccess;
}
答案1
得分: 1
以下是翻译好的部分:
最简单的方法是使用方法引用 Callable<Boolean> isPageLoaded = this::isPageLoaded
,或者显式地将其用作 Lambda 表达式 Callable<Boolean> isPageLoaded = () -> isPageLoaded();
这会看起来像这样:
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(this::isPageLoaded);
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> isPageLoaded());
另一种方法是将您的方法定义为返回 Callable<Boolean>
,然后使用 Lambda 语法 () -> {}
来编写可调用对象。
protected Callable<Boolean> isPageLoaded() {
return () -> {
String jsQuery = "function pageLoaded() "
+ "{var loadingStatus=(document.readyState=='complete');"
+ "return loadingStatus;};"
+ "return pageLoaded()";
boolean isSuccess = false;
try {
isSuccess = (Boolean) evaluateJavascript(jsQuery);
} catch (Exception exp) {
exp.printStackTrace();
}
return isSuccess;
};
}
Lambda 表达式和方法引用可以是非常强大的工具。
<details>
<summary>英文:</summary>
The easiest way to do that is to use a method reference `Callable<Boolean> isPageLoaded = this::isPageLoaded`, or to use it explicitly as lambda `Callable<Boolean> isPageLoaded = () -> isPageLoaded();`
This would look like
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(this::isPageLoaded);
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> isPageLoaded());
Another way would be to define your method as returning a `Callable<Boolean>` and then using lambda syntax `() -> {}` to write the callable.
protected Callable<Boolean> isPageLoaded() {
return () -> {
String jsQuery = "function pageLoaded() "
+ "{var loadingStatus=(document.readyState=='complete');"
+ "return loadingStatus;};"
+ "return pageLoaded()";
boolean isSuccess = false;
try {
isSuccess = (Boolean) evaluateJavascript(jsQuery);
} catch (Exception exp) {
exp.printStackTrace();
}
return isSuccess;
};
}
Lambda expressions and method references can be quite powerful tools.
</details>
# 答案2
**得分**: 0
只需将您的 `isPageLoaded()` 方法定义如下:
protected Callable<Boolean> isPageLoaded() {
//代码...
}
<details>
<summary>英文:</summary>
Just define your `isPageLoaded()` method as follows:
protected Callable<Boolean> isPageLoaded() {
//code..
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论