How to get a view from within Espresso to pass into an IdlingResource?

EGHDK picture EGHDK · Jan 8, 2016 · Viewed 13.1k times · Source

I essentially have a custom IdlingResource that takes a View a constructor argument. I can't find anywhere that really talks about how to implement it.

I'm trying to use this answer: https://stackoverflow.com/a/32763454/1193321

As you can see, it takes a ViewPager, but when I'm registering the IdlingResource in my test class, I'm not sure how I can get my view.

I've tried findViewById() and I've tried getting the currently running activity and then calling findViewById() on that, with no luck.

Anyone know what to do in this scenario?

Answer

EGHDK picture EGHDK · Jan 14, 2016

Figured it out. To get the view to pass into an idling resource, all you have to do is take the member variable of your ActivityTestRule

For example:

@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(
        MainActivity.class);

and then just call getActivity().findViewById(R.id.viewId)

So the end result is:

activityTestRule.getActivity().findViewById(R.id.viewId);