等待 Espresso 测试中的异步操作

Wait for asynchronous operation inside Espresso test

我有 espresso 测试 Android:

        @Test
        public void ExternalBankAccountDetailsFragment_InfoShown() throws InterruptedException {

            // SETUP
            mockRequest();
            clickOnItem();
mActivityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            Thread.sleep(100);

            // CHECKS
    onView(withId(R.id.view)).check(matches(hasDescendant(withText("test"))));
        }

我需要使用 Thread.sleep 来等待 async 调用。但我知道这不是一个好的做法。我熟悉 IdlingResources,但我不能使用它,因为我必须更改我的代码才能使用它(例如 callback 中的 CountingIdlingResource 用法等)。 如何避免使用 Thread.sleep()?

最终所有解决方案都会做类似 Thread.sleep 的事情。问题是:你怎么能只等待所需的最短时间?

空闲资源的一个替代方法是这个微型 ConditionWatcher 库:https://github.com/AzimoLabs/ConditionWatcher

您可以创建一个 Instruction 等待检查当前 activity 是否可以找到视图示例