Kaspresso. 如何在不抛出异常的情况下检查元素是否显示。

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

Kaspresso. How check element is displayed without exception

问题

我想检查 authauthorization 屏幕是否显示在 kaspresso 中
isDisplyed() 不返回布尔值,并且我遇到了错误。

如何在不引发异常的情况下检查元素是否显示

现在我的解决方案:

fun isNeedAuth(): Boolean {
    return try {
        authButton.isEnabled()
        true
    } catch (e: NullPointerException) {
        false
    }
}
英文:

I want check authauthorization screen is displayed with kaspresso
But isDisplyed() dont return boolean, and i have error.

How check element is displayed without exception

Now my solution:

fun isNeedAuth(): Boolean {
        return try {
            authButton.isEnabled()
            true
        } catch (e: NullPointerException) {
            false
        }
    }

答案1

得分: 0

我个人正在使用:

fun safeAssertion(assert: () -> Unit) =
    try {
        assert()
        true
    } catch (_: Throwable) {
        false
    }

在测试中看起来像这样:

if (safeAssertion { authButton.isEnabled() }) { doSomething() }

此外,您可以为 KViews 创建一个扩展:

fun <K : BaseAssertions> K.safeAssert(assert: K.() -> Unit) = safeAssertion { assert() }

并像这样使用它:

if (authButton.safeAssert { isDisplayed() }) { doSomething() }

使用等待所需状态的更高级解决方案:

fun waitUntilConditionMet(
    description: String = "",
    maxWaitTime: Duration = 10.seconds,
    retryInterval: Duration = 250.milliseconds,
    onFail: () -> Unit = { throw MyCustomWaitingFailedException(maxWaitTime, description) },
    conditionToMeet: () -> Boolean
) {
    try {
        runBlocking {
            withTimeout(maxWaitTime) {
                while (!conditionToMeet()) {
                    delay(retryInterval)
                }
            }
        }
    } catch (e: Throwable) {
        Log.e("My Custom Waiter", e.message ?: "Failed to meet condition in ${maxWaitTime.inWholeSeconds} seconds")
        onFail()
    }
}

使用示例(如果在10秒内不满足条件,则会抛出异常):

waitUntilConditionMet("Some screen appeared") {
    safeAssertion {
        someButton.isDisplayed()
        anotherView.isEnabled()
    }
}

或者不抛出异常的情况下:

waitUntilConditionMet(
    description = "Some screen appeared",
    onFail = { doSomethingIfConditionIsNotMet() }
) {
    safeAssertion {
        someButton.isDisplayed()
        anotherView.isEnabled()
    }
}
英文:

I'm personally using:

fun safeAssertion(assert: () -&gt; Unit) =
    try {
        assert()
        true
    } catch (_: Throwable) {
        false
    }

In tests it looks like:

if (safeAssertion { authButton.isEnabled() }) { doSomething() }

Also, you can make an extension for KViews:

fun &lt;K : BaseAssertions&gt; K.safeAssert(assert: K.() -&gt; Unit) = safeAssertion { assert() }

And use it like:

if (authButton.safeAssert { isDisplayed() }) { doSomething() }

More advanced solution with waiting for desired state:

fun waitUntilConditionMet(
    description: String = &quot;&quot;,
    maxWaitTime: Duration = 10.seconds,
    retryInterval: Duration = 250.milliseconds,
    onFail: () -&gt; Unit = { throw MyCustomWaitingFailedException(maxWaitTime, description) },
    conditionToMeet: () -&gt; Boolean
) {
    try {
        runBlocking {
            withTimeout(maxWaitTime) {
                while (!conditionToMeet()) {
                    delay(retryInterval)
                }
            }
        }
    } catch (e: Throwable) {
        Log.e(&quot;My Custom Waiter&quot;, e.message ?: &quot;Failed to meet condition in ${maxWaitTime.inWholeSeconds} seconds&quot;)
        onFail()
    }
}

Usage example (will throw exception if condition is not met in 10 seconds):

waitUntilConditionMet(&quot;Some screen appeared&quot;) {
    safeAssertion {
        someButton.isDisplayed()
        anotherView.isEnabled()
    }
}

Or without throwing exception:

waitUntilConditionMet(
    description = &quot;Some screen appeared&quot;,
    onFail = { doSomethingIfConditionIsNotMet() }
) {
    safeAssertion {
        someButton.isDisplayed()
        anotherView.isEnabled()
    }
}

huangapple
  • 本文由 发表于 2023年5月30日 02:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359682.html
匿名

发表评论

匿名网友

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

确定