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;
}
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.