Espresso:如何测试 SwipeRefreshLayout?
Posted
技术标签:
【中文标题】Espresso:如何测试 SwipeRefreshLayout?【英文标题】:Espresso: How to test SwipeRefreshLayout? 【发布时间】:2015-11-03 17:55:26 【问题描述】:在SwipeRefreshLayout
上完成向下滑动时,我的应用会重新加载数据。现在我尝试使用 android Test Kit / Espresso 进行测试,如下所示:
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: "SwipeRefreshLayoutid=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 确实可以应该对其进行一些操作。
【问题讨论】:
如何用 kakao 框架做到这一点 【参考方案1】:睡在上面有时会有所帮助。根本原因是要滑动的视图对用户来说只有 89% 可见,而 Espresso 的滑动操作在内部需要 90%。所以解决方案是将滑动动作包装到另一个动作中并手动覆盖这些约束,如下所示:
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?的主要内容,如果未能解决你的问题,请参考以下文章
Espresso:如何测试 ImageButton 的背景可绘制对象
如何在 Espresso 中重新运行失败的测试? - 头脑风暴