英文:
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: () -> 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 <K : BaseAssertions> K.safeAssert(assert: K.() -> Unit) = safeAssertion { assert() }
And use it like:
if (authButton.safeAssert { isDisplayed() }) { doSomething() }
More advanced solution with waiting for desired state:
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()
}
}
Usage example (will throw exception if condition is not met in 10 seconds):
waitUntilConditionMet("Some screen appeared") {
safeAssertion {
someButton.isDisplayed()
anotherView.isEnabled()
}
}
Or without throwing exception:
waitUntilConditionMet(
description = "Some screen appeared",
onFail = { doSomethingIfConditionIsNotMet() }
) {
safeAssertion {
someButton.isDisplayed()
anotherView.isEnabled()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论