使用 Espresso 在 Android 上测试进度条 [英] Testing progress bar on Android with Espresso

查看:32
本文介绍了使用 Espresso 在 Android 上测试进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作流程应该如下:

  1. 活动开始
  2. 进度条可见
  3. 触发网络请求(空闲资源已注册,因此 espresso 知道如何等待).
  4. 隐藏进度条
  5. 显示来自网络的文本.

到目前为止,我已经为步骤 1、3、5 编写了断言并且它完美地工作:

onView(withText("foo 1")).check(matches(isDisplayed()));

问题是,我不知道如何让 espresso 知道验证进度条的可见性之前请求被提出和之后请求被提出.>

考虑 onCreate() 方法如下:

super.onCreate(...);setContentView(...);showProgressBar(true);apiClient.getStuff(新回调(){公共无效 onSuccess() {showProgressBar(false);}});

我尝试了以下方法,但不起作用:

//此时活动启动.activityRule.launchActivity(new Intent());//到此为止,请求已被触发,响应已被触发//返回,所以进度条现在消失了.onView(withId(R.id.progress_bar)).check(matches(isDisplayed()));onView(withId(R.id.progress_bar)).check(matches(not(isDisplayed())));

发生这种情况的原因是,由于客户端注册为空闲资源,espresso 将等到它再次空闲,然后再运行第一个 onView(...progressbar...)... 所以我需要一种方法让 espresso 知道在 BEFORE 进入空闲状态之前运行它.

这也不起作用:

idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {@覆盖公共无效 onTransitionToIdle() {onView(withId(R.id.progress_bar)).check(matches(isDisplayed()));}});

解决方案

Espresso 有动画问题.您可以将进度条的 drawable 设置为静态的东西,仅用于测试,它会按预期工作.

Drawable notAnimatedDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.whatever);((ProgressBar) getActivity().findViewById(R.id.progress_bar)).setIndeterminateDrawable(notAnimatedDrawable);onView(withId(R.id.progress_bar)).check(matches(isDisplayed()));

The workflow should be the following:

  1. Activity starts
  2. Progress bar is visible
  3. Network request fires (idling resource is already registered so espresso knows how to wait for it).
  4. Progress bar is hidden
  5. Text from network is shown.

Up to this point, I have written assertions for steps 1, 3, 5 and it works perfectly:

onView(withText("foo 1"))
    .check(matches(isDisplayed()));

Problem is, I have no idea how to let espresso know to verify the visibility of progress bar before the request is made and after the request is made.

Consider the onCreate() method is the following:

super.onCreate(...);
setContentView(...);

showProgressBar(true);
apiClient.getStuff(new Callback() {
    public void onSuccess() {
        showProgressBar(false);
    }
});

I have tried the following but it doesn't work:

// Activity is launched at this point.
activityRule.launchActivity(new Intent());

// Up to this point, the request has been fired and response was 
// returned, so the progress bar is now GONE.
onView(withId(R.id.progress_bar))
   .check(matches(isDisplayed()));

onView(withId(R.id.progress_bar))
    .check(matches(not(isDisplayed())));

The reason this is happening is because, since the client is registered as an idling resource, espresso will wait until it is idle again before running the first onView(...progressbar...)... so I need a way to let espresso know to run that BEFORE going to idle.

EDIT: this doesn't work either:

idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
        @Override
        public void onTransitionToIdle() {
            onView(withId(R.id.progress_bar))
                    .check(matches(isDisplayed()));
        }
    });

解决方案

Espresso has problems with the animation. You can just set the drawable of the progress bar to something static just for the test and it works as expected.

Drawable notAnimatedDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.whatever);
((ProgressBar) getActivity().findViewById(R.id.progress_bar)).setIndeterminateDrawable(notAnimatedDrawable);

onView(withId(R.id.progress_bar)).check(matches(isDisplayed()));

这篇关于使用 Espresso 在 Android 上测试进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆