如何使用Espresso检查Viewpager项目ID? [英] How to check Viewpager item ids with Espresso?

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

问题描述

我有一个Viewpager,它由相同片段视图的副本组成.您可以在它们之间滑动.我正在编写Espresso测试并尝试对每个页面的ID进行断言,但是它们显然是模棱两可的,因为加载了多个页面,并且它们都共享相同的ID.我不想将视图分页器更改为一次仅加载一页.在我的应用程序中,这样做效率不高.

I have a Viewpager that's composed of copies of the same fragment view. You can swipe between them. I'm writing an Espresso test and trying to assert on ids of each page, but they are obviously ambiguous because there are multiple pages loaded and they all share the same ids. I don't want to change the view pager to load only 1 page at a time. In my application, it's inefficient to do so.

我还考虑过使用标签,并为每个视图添加一个唯一的字符串,但是我只是在修改源代码,以便测试可以正常工作.我不喜欢那样使用Espresso检查viewpager页面内内容的正确方法是什么?

I've also thought of using tags and tag each view with a unique string, but then I'm modifying the source code only so that my test works. I don't like that. What is the correct way of checking content inside the page of a viewpager with Espresso?

推荐答案

假定一次只显示一个页面,则可以使用swipeLeft/swipeRight移至页面,然后检查是否在其中显示了正确的内容.该页面.

Assuming that only one of the pages is displayed at a time you can use swipeLeft/swipeRight to move to a page and then check that the right thing is displayed in that page.

例如

 onView(withId(R.id.pager))
   .perform(swipeLeft())
   .check(matches(hasDescendant(withText("Position #2"))));

这篇关于如何使用Espresso检查Viewpager项目ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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