Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Esspresso测试中等待视图消失_Android_Android Espresso - Fatal编程技术网

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)