gpt4 book ai didi

java - 如何在 Espresso 中等待异步事件?

转载 作者:行者123 更新时间:2023-12-02 11:38:25 24 4
gpt4 key购买 nike

我很困惑,Espresso 在处理失败时似乎有点不一致。

我想要实现的是重试检查要显示的某个字符串。单击按钮会启动异步操作,该操作应在某个时间范围内完成,并且我可以成功地在检查某些元素是否存在的周围放置一个 try catch block :

try {
onView(withId(fieldIdToBeAvailable))
.inRoot(isDialog())
.check(matches(isDisplayed()));
}
catch (NoMatchingViewException nmve) {
// ok, retry this or fail
retry();
}

这会抛出 NoMatchingViewException 并使用某种循环重试此操作,但如果我尝试检查匹配的文本,例如 Espresso 不会抛出异常:

onView(withId(R.id.sync_status))
.check(matches(withText("Logged in")));

唯一发生的事情是 android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:

有人会如何围绕这个编写重试概念?当我将 failureHandler 放置到位时,我无法抛出它(因此我会捕获异常而不是评估 AssertionFailed 错误),因为 handle 不允许抛出:

        onView(withId(R.id.sync_status)).withFailureHandler(new FailureHandler()
{
@Override
public void handle(Throwable error, Matcher<View> viewMatcher)
{
// Compiler error: Unhandled exception
throw new Exception("Failed, try again");
}
}).check(matches(withText("Logged in")));

最佳答案

我建议使用空闲资源来处理异步操作 https://developer.android.com/training/testing/espresso/idling-resource.html

无论如何,Exception 是一个已检查的异常,因此应该用 try-catch 包装它,或者方法应该使用 throws 关键字定义该异常。要“修复”编译器错误,您可以使用 RuntimeExceptionError

关于java - 如何在 Espresso 中等待异步事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48753981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com