1

I am using espresso to perform several tests. In my latest test I have to test an activity that has a viewPager with fragments.

What I want to do is to call a method of the current visible fragment from the test.

Is it possible to do this with espresso? I know how to create ViewActions and Matchers but what I need to call the method of the fragment itself and not on a view.

Thanks in advance.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • 1
    Yes, http://stackoverflow.com/questions/18609261/getting-the-current-fragment-instance-in-the-viewpager – nenick Jun 14 '16 at 05:48
  • Thanks for the answer. So, the only way to do this is without espresso code? – jeprubio Jun 14 '16 at 07:28
  • 2
    What kind of espresso code do you expect? There is no native support to call custom methods. But you can try to implement your own ViewAction `onView(withId(R.id.pager)).perform(new ViewAction { ... })` and then on the view call something like `pager.getItem(pager.getCurrentItem())`. – nenick Jun 14 '16 at 07:59
  • Yes, it works. By calling a ViewAction on a ViewPager matcher you can access this way to its current fragment and execute the public method you want. I wonder why I didn't see it. You've saved my day. Thank you very much. – jeprubio Jun 14 '16 at 09:34

0 Answers0