Android 如何在Esspresso测试中等待视图消失
我有一个Android 如何在Esspresso测试中等待视图消失,android,android-espresso,Android,Android Espresso,我有一个文本视图,显示一个“正在加载”字符串。。。我需要等到这景色消失。。。我没有Asynctask的句柄,因为此方法正在IntentService中运行,并在加载完成时发送广播 你知道如何在浓缩咖啡测试中等待视图状态的改变吗?我将需要与一些字符串,将改变,需要等待那个相同的。。。我想是同一个案子 谢谢你的帮助。网上没有太多的例子或常见问题。这已经得到了回答 您可以通过向Espresso注册web服务的IdlingResource来处理这种情况。看一看 最有可能的情况是,您希望使用Countin
文本视图
,显示一个“正在加载”字符串。。。我需要等到这景色消失。。。我没有Asynctask
的句柄,因为此方法正在IntentService
中运行,并在加载完成时发送广播
你知道如何在浓缩咖啡测试中等待视图状态的改变吗?我将需要与一些字符串,将改变,需要等待那个相同的。。。我想是同一个案子
谢谢你的帮助。网上没有太多的例子或常见问题。这已经得到了回答
您可以通过向Espresso注册web服务的IdlingResource来处理这种情况。看一看
最有可能的情况是,您希望使用CountingIdlingResource(它使用一个简单的计数器来跟踪什么是空闲的)。此示例测试演示了如何执行此操作。您可以定义一个
视图操作
,该操作持续循环主线程,直到相关视图的可见性更改为视图。消失了
或经过了最长时间
首先,定义ViewAction
,如下所示:
/**
* A [ViewAction] that waits up to [timeout] milliseconds for a [View]'s visibility value to change to [View.GONE].
*/
class WaitUntilGoneAction(private val timeout: Long) : ViewAction {
override fun getConstraints(): Matcher<View> {
return any(View::class.java)
}
override fun getDescription(): String {
return "wait up to $timeout milliseconds for the view to be gone"
}
override fun perform(uiController: UiController, view: View) {
val endTime = System.currentTimeMillis() + timeout
do {
if (view.visibility == View.GONE) return
uiController.loopMainThreadForAtLeast(50)
} while (System.currentTimeMillis() < endTime)
throw PerformException.Builder()
.withActionDescription(description)
.withCause(TimeoutException("Waited $timeout milliseconds"))
.withViewDescription(HumanReadables.describe(view))
.build()
}
}
/**
* @return a [WaitUntilGoneAction] instance created with the given [timeout] parameter.
*/
fun waitUntilGone(timeout: Long): ViewAction {
return WaitUntilGoneAction(timeout)
}
onView(withId(R.id.loadingTextView)).perform(waitUntilGone(3000L))
第三,也是最后一点,在测试方法中调用此ViewAction
,如下所示:
/**
* A [ViewAction] that waits up to [timeout] milliseconds for a [View]'s visibility value to change to [View.GONE].
*/
class WaitUntilGoneAction(private val timeout: Long) : ViewAction {
override fun getConstraints(): Matcher<View> {
return any(View::class.java)
}
override fun getDescription(): String {
return "wait up to $timeout milliseconds for the view to be gone"
}
override fun perform(uiController: UiController, view: View) {
val endTime = System.currentTimeMillis() + timeout
do {
if (view.visibility == View.GONE) return
uiController.loopMainThreadForAtLeast(50)
} while (System.currentTimeMillis() < endTime)
throw PerformException.Builder()
.withActionDescription(description)
.withCause(TimeoutException("Waited $timeout milliseconds"))
.withViewDescription(HumanReadables.describe(view))
.build()
}
}
/**
* @return a [WaitUntilGoneAction] instance created with the given [timeout] parameter.
*/
fun waitUntilGone(timeout: Long): ViewAction {
return WaitUntilGoneAction(timeout)
}
onView(withId(R.id.loadingTextView)).perform(waitUntilGone(3000L))
您可以采用这个概念,类似地创建一个WaitForTextAction
类,该类将等待TextView
的文本更改为某个值。但是,在这种情况下,您可能需要将getConstraints()
函数返回的Matcher
从any(View::class.java)
更改为any(TextView::class.java)