3

Is it possible to click the first item of a ListView inside a ViewPager?

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. How perfom action to a button of an Item? you cant use onData for a ViewPager. Plus if I run the Test i get a AmbiguousViewMatcherException for multiple matches.

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()); the first item is never clicked.

Is there another way to click the first item of the list?

Community
  • 1
  • 1
elpatricko
  • 488
  • 5
  • 17

1 Answers1

6

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.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • it worked, for one run... then i got another error: `android.support.test.espresso.PerformException: Error performing 'load adapter data' on view '(with id: my.awesome.app:id/myListView and at least 100 percent of the view's area is displayed to the user.)'.` – elpatricko Aug 19 '16 at 07:40
  • Are you swiping the ViewPager before clicking this first element? If so, add a SystemClock.sleep(800); before this instruction and check if it works. – jeprubio Aug 19 '16 at 07:47
  • Then, if it works, you can use this idling resource: https://github.com/Karumi/Rosie/blob/master/sample/src/androidTest/java/com/karumi/rosie/sample/idlingresources/ViewPagerIdlingResource.java to avoid the sleep and should also work. – jeprubio Aug 19 '16 at 07:49
  • Okay it gets more confusing for me. The first Item is clicked every time if i use this combination: `onView(allOf(withId(R.id.myListView), isDisplayed())).perform(click()); onData(anything()).inAdapterView(allOf(withId(R.id.myListView),isCompletelyDisplayed())).atPosition(0).perform(click());` . If i change the position, the first item is still clicked. – elpatricko Aug 19 '16 at 07:51
  • That means that the listview is only completely displayed if you previously click on that. Try also using isDisplayed() instead of isCompletelyDisplayed(), does it throw an AmbiguousViewMatcherException or does it work? – jeprubio Aug 19 '16 at 07:52
  • Maybe, the best solution to your problem might be finding the two "myListView" that you have in your code and maybe rename one of them if they are not the same. It should work if you have two fragments on your viewpager with the same id (myListView) on them and the lists are different. I need more info about your layout to be sure. In that case the isCompletelyDisplayed would be not needed after renaming the id of one of the lists. – jeprubio Aug 19 '16 at 08:00
  • I cant rename the other listview because its the same. Its just filled with other data in a different "tab". i'll take the two click solution for now and look back at the problem later. thanks for your help! – elpatricko Aug 19 '16 at 08:30