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

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

问题描述

工作流程应为以下内容:

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

到目前为止,我已经为步骤 1、3、5 编写了断言,并且效果很好:

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

问题是,我不知道如何在发出请求之前 和发出请求后之后让浓缩咖啡知道进度条的可见性.

考虑onCreate()方法如下:

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

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

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

// 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())));

发生这种情况的原因是,由于客户端已注册为空闲资源,因此espresso在运行第一个onView(...progressbar...)...之前将再次等待,直到它成为 idle ,因此我需要一种方法意式浓缩咖啡知道要运行之前处于空闲状态.

编辑:这也不起作用:

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

解决方案

Espresso的动画存在问题.您可以仅将进度条的drawable设置为static(仅用于测试),即可按预期工作.

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天全站免登陆