简体   繁体   English

浓咖啡:在ViewPager内单击ListView中的第一项

[英]Espresso: click first item in ListView inside a ViewPager

Is it possible to click the first item of a ListView inside a ViewPager? 是否可以在ViewPager中单击ListView的第一项?

I already tried to get it working like this: 我已经尝试过使它像这样工作:

onData(anything()).inAdapterView(withId(R.id.myListView)).atPosition(0).perform(click()); .

But as I understand from here: Testing ViewPager with Espresso. 但是据我了解: 用Espresso测试ViewPager。 How perfom action to a button of an Item? 如何对项目的按钮执行操作? you cant use onData for a ViewPager. 您不能将onData用于ViewPager。 Plus if I run the Test i get a AmbiguousViewMatcherException for multiple matches. 另外,如果我运行测试,则会得到多次匹配的AmbiguousViewMatcherException。

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: my.awesome.app:id/myListView' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

If i run the test like this onView(allOf(withId(R.id.myListView),isDisplayed())).perform(click()); 如果我像这样运行测试onView(allOf(withId(R.id.myListView),isDisplayed())).perform(click()); the first item is never clicked. 第一项永远不会被点击。

Is there another way to click the first item of the list? 还有另一种方法可以单击列表的第一项吗?

try this: 尝试这个:

onData(anything())
    .inAdapterView(allOf(withId(R.id.myListView), isCompletelyDisplayed()))
    .atPosition(0).perform(click());

As you have an AmbiguousViewMatcherException this might work matching the "myListView" which is displayed on the screen if I'm not wrong. 如果您输入的是AmbiguousViewMatcherException,那么如果我没有记错的话,这可能与屏幕上显示的“ myListView”匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM