Espresso:如何测试SwipeRefreshLayout? [英] Espresso: How to test SwipeRefreshLayout?

查看:48
本文介绍了Espresso:如何测试SwipeRefreshLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SwipeRefreshLayout上进行向下滑动时,我的应用程序会重新加载数据.现在,我尝试使用Android测试工具包/Espresso像这样测试它:

My app reloads data when a down-swipe is done on a SwipeRefreshLayout. Now I try to test this with Android Test Kit / Espresso like this:

onView(withId(R.id.my_refresh_layout)).perform(swipeDown());

不幸的是,这失败了

android.support.test.espresso.PerformException: Error performing 'fast swipe'
on view 'with id: my.app.package:id/my_refresh_layout'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view
does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "SwipeRefreshLayout{id=2131689751, res-name=my_refresh_layout, visibility=VISIBLE,
width=480, height=672, has-focus=false, has-focusable=true, has-window-focus=true,
is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0,
child-count=2}"

当然,布局是可见的并且可以手动滑动,但是我不确定是否做错了什么?布局跨越整个屏幕,因此应该实际上,Espresso可以对它进行一些操作.

Of course, the layout is visible and swiping manually works, but I'm unsure if I'm doing something wrong? The layout spans the whole screen, so it should really be possible for Espresso to do some action on it.

推荐答案

有时睡觉会有所帮助.根本原因是,要滑动的视图仅对用户可见89%,而Espresso的滑动操作内部需要90%.因此解决方案是将滑动动作包装到另一个动作中,并手动覆盖这些约束,如下所示:

Sleeping over it sometimes helps. The underlying reason was that the to-be-swiped view was only 89% visible to the user, while Espresso's swipe actions internally demand 90%. So the solution is to wrap the swipe action into another action and override these constraints by hand, like this:

public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return constraints;
        }

        @Override
        public String getDescription() {
            return action.getDescription();
        }

        @Override
        public void perform(UiController uiController, View view) {
            action.perform(uiController, view);
        }
    };
}

然后可以这样称呼它:

onView(withId(R.id.my_refresh_layout))
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85));

这篇关于Espresso:如何测试SwipeRefreshLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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