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

时间:2019-11-20 08:04:57

标签: android-espresso

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

2 个答案:

答案 0 :(得分:1)

///我使用以下简单代码(没有IdlingResource操作系统自定义匹配器)。其中的viewId是要在ProgressBar消失后要验证的视图

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

答案 1 :(得分:0)

因此,您要等到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的特定ID更改ProgressBar,并在必要时更新5秒(5000毫秒)的超时时间。

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