这是关于浓缩咖啡的。我在模拟器上成功运行集成测试。我认为有些测试失败是因为它运行得太快。有没有办法减慢执行/播放速度?
测试失败是不可能导致速度加快的。 Espresso 可以将所有测试操作与被测应用程序同步。默认情况下,Espresso 等待当前消息队列中的 UI 事件进行处理,并默认
AsyncTasks
完成,然后再继续下一个测试操作。但是,如果这对于您的应用程序来说还不够,您可以告诉 Espresso 何时空闲、何时不空闲。为此,您必须:
Espresso.registerIdlingResource
向 Espresso 注册一个或多个 IdlingResource。如果您需要更多帮助,请问我!!
当您在 Android Studio 中录制 Espresso 测试时,当存在视图交互来处理延迟时,它会自动将 Sleep 语句添加到测试中。这是与评论一起生成的方法:
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
哈哈...其实Espresso 就只有这样的作用。您面临的问题是 UI 事件无法完成(例如,在从网络调用加载列表之前单击列表项)。 在这种情况下,如果您的资源是从其他线程加载的,您实际上可以执行 Thread.sleep(millis) 或更有效地执行 UiController 的 LoopMainThreadForAtleast(millis) 方法来等待
something to load
(事件完成)。
之前的所有建议都是有问题的,因为您的睡眠要么非常保守,要么根据平台的性能出现虚假故障。相反,尝试一个循环,等待视图资源可用(如果一段时间后不可用,则会出错)。我在底部放置了一个功能供您使用。
您可以像这样使用此代码(例如填写文本字段):
waitForView { onView(withId(YOUR_VIEW_ID)).perform(clearText(),typeText("TEST TEXT"), pressImeActionButton(), pressBack()) }
fun<T> waitForView(time: Int = 5000, thunk: ()->T) : T
{
var countup = 0
while(true) try
{
return thunk()
}
catch (e: NoMatchingViewException)
{
if (countup >= time)
{
println("After delay of $time, there is still no matching view")
throw e
}
sleep(500)
countup += 500
}
catch (e: PerformException)
{
sleep(500)
countup += 500
}
}