我想检查authauthorization屏幕显示kaspresso但isDisplyed()不返回布尔值,我有错误。如何无例外地显示check元素现在我的解决方案:
isDisplyed()
fun isNeedAuth(): Boolean { return try { authButton.isEnabled() true } catch (e: NullPointerException) { false } }
fun isNeedAuth(): Boolean {
return try {
authButton.isEnabled()
true
} catch (e: NullPointerException) {
false
}
8ljdwjyq1#
我个人使用:
fun safeAssertion(assert: () -> Unit) = try { assert() true } catch (_: Throwable) { false }
fun safeAssertion(assert: () -> Unit) =
try {
assert()
} catch (_: Throwable) {
在测试中,它看起来像:
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() }}
fun waitUntilConditionMet(
description: String = "",
maxWaitTime: Duration = 10.seconds,
retryInterval: Duration = 250.milliseconds,
onFail: () -> Unit = { throw MyCustomWaitingFailedException(maxWaitTime, description) },
conditionToMeet: () -> Boolean
) {
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("Some screen appeared") {
safeAssertion {
someButton.isDisplayed()
anotherView.isEnabled()
或者不抛出异常:
waitUntilConditionMet( description = "Some screen appeared", onFail = { doSomethingIfConditionIsNotMet() }) { safeAssertion { someButton.isDisplayed() anotherView.isEnabled() }}
waitUntilConditionMet(
description = "Some screen appeared",
onFail = { doSomethingIfConditionIsNotMet() }
1条答案
按热度按时间8ljdwjyq1#
我个人使用:
在测试中,它看起来像:
此外,您还可以为KViews创建扩展:
并使用它像:
等待所需状态的更高级解决方案:
用法示例(如果条件在10秒内未满足,则将引发异常):
或者不抛出异常: