有什么方法可以等到浓缩咖啡测试中的不可见进度条?

any method to wait until invisible progressbar in espresso test?

例如。我创建了两个活动,第二个活动包含回收器项目列表和进度条。直到完成 API 调用,我们正在等待响应并显示进度条

因此,您想等到 ProgressBar 被隐藏。 您可以创建一个 idling resource 或使用自定义 ViewAction 作为这个:

/**
 * Perform action of waiting until the element is accessible & not shown.
 * @param viewId The id of the view to wait for.
 * @param millis The timeout of until when to wait for.
 */
public static ViewAction waitUntilNotShown(final int viewId, final long millis) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "wait for a specific view with id <" + viewId + "> is hidden during " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + millis;
            final Matcher<View> viewMatcher = withId(viewId);

            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    // found view with required ID
                    if (viewMatcher.matches(child) && !child.isShown()) {
                        return;
                    }
                }

                uiController.loopMainThreadForAtLeast(50);
            }
            while (System.currentTimeMillis() < endTime);

            // timeout happens
            throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(new TimeoutException())
                    .build();
        }
    };
}

你可以这样使用它:

onView(isRoot()).perform(waitUntilNotShown(R.id.theIdToWaitFor, 5000));

theIdToWaitFor 更改为您的 ProgressBar 的特定 ID,并在必要时将超时更新为 5 秒(5000 毫秒)。

但是,根据您正在进行的测试,如果这不是集成测试,最好不要进行真正的 api 调用。

//我使用这个简单的代码(没有 IdlingResource os 自定义匹配器)。 ProgressBar 消失后,viewId 是您要验证的视图

fun viewIsDisplayedAfterProgressDialogIsGone(viewId: Int){
    onView(withId(viewId))
        .inRoot(not(RootMatchers.isDialog()))
        .check(matches(isDisplayed()))
}

这只是 的改编版本。

/**
 * Wait until the element is accessible and not shown.
 * @param millis The maximum timeout to wait for.
 */
fun waitUntilNotShown(millis: Long): ViewAction {
    return object : ViewAction {
        override fun getConstraints(): Matcher<View> {
            return any(View::class.java)
        }

        override fun getDescription(): String {
            return "Wait for a specific view to be hidden, for a max of $millis ms."
        }

        override fun perform(uiController: UiController, view: View) {
            uiController.loopMainThreadUntilIdle()

            val startTime = System.currentTimeMillis()
            val endTime = startTime + millis

            do {
                if (!view.isShown) {
                    return
                }
                uiController.loopMainThreadForAtLeast(100)
            } while (System.currentTimeMillis() < endTime)

            throw PerformException.Builder()
                .withActionDescription(this.description)
                .withViewDescription(HumanReadables.describe(view))
                .withCause(TimeoutException())
                .build()
        }
    }
}

那么我们可以这样使用:

onView(withId(R.id.welcome_progress_bar))
  .perform(waitUntilNotShown(3_000))

我进行了此更改,因为我在使用 onView(isRoot()) 时遇到错误。