方法返回类型设为`Callable`。

huangapple go评论80阅读模式
英文:

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&lt;Boolean&gt;.

Please help me to make the method isPageLoaded() return Callable&lt;Boolean&gt;

Here is the implementation of isPageLoaded() method:

protected Boolean isPageLoaded() {
    String jsQuery = &quot;function pageLoaded() &quot;
            + &quot;{var loadingStatus=(document.readyState==&#39;complete&#39;);&quot;
            + &quot;return loadingStatus;};&quot;
            + &quot;return pageLoaded()&quot;;

    boolean isSuccess = false;
    try {
        isSuccess = (Boolean) evaluateJavascript(jsQuery);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
    return isSuccess;
}

答案1

得分: 1

以下是翻译好的部分:

最简单的方法是使用方法引用 Callable&lt;Boolean&gt; isPageLoaded = this::isPageLoaded,或者显式地将其用作 Lambda 表达式 Callable&lt;Boolean&gt; isPageLoaded = () -&gt; isPageLoaded();

这会看起来像这样:

Awaitility.await().atMost(5, TimeUnit.SECONDS).until(this::isPageLoaded);
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -&gt; isPageLoaded());

另一种方法是将您的方法定义为返回 Callable&lt;Boolean&gt;,然后使用 Lambda 语法 () -&gt; {} 来编写可调用对象。

protected Callable&lt;Boolean&gt; isPageLoaded() {
    return () -&gt; {
        String jsQuery = &quot;function pageLoaded() &quot;
            + &quot;{var loadingStatus=(document.readyState==&#39;complete&#39;);&quot;
            + &quot;return loadingStatus;};&quot;
            + &quot;return pageLoaded()&quot;;

        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&lt;Boolean&gt; isPageLoaded = this::isPageLoaded`, or to use it explicitly as lambda `Callable&lt;Boolean&gt; isPageLoaded = () -&gt; isPageLoaded();`

This would look like

    Awaitility.await().atMost(5, TimeUnit.SECONDS).until(this::isPageLoaded);
    Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -&gt; isPageLoaded());

Another way would be to define your method as returning a `Callable&lt;Boolean&gt;` and then using lambda syntax `() -&gt; {}` to write the callable.

    protected Callable&lt;Boolean&gt; isPageLoaded() {
        return () -&gt; {
            String jsQuery = &quot;function pageLoaded() &quot;
                + &quot;{var loadingStatus=(document.readyState==&#39;complete&#39;);&quot;
                + &quot;return loadingStatus;};&quot;
                + &quot;return pageLoaded()&quot;;
    
            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&lt;Boolean&gt; isPageLoaded() {
        //代码...
    }

<details>
<summary>英文:</summary>

Just define your `isPageLoaded()` method as follows:

    protected Callable&lt;Boolean&gt; isPageLoaded() {
        //code..
    }

</details>



huangapple
  • 本文由 发表于 2020年8月1日 16:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63203211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定