In an Espresso test, I want to get reference to my ViewPager
so I can attach an IdlingResource
to it. But findViewById
returns null
, even though I confirm just before that the ViewPager
is displayed.
// Passes
onView(withId(R.id.viewPager)).check(matches(isDisplayed()));
Activity activity = activityTestRule.getActivity();
assertNotNull(activity);
// Fails
assertNotNull(activity.findViewById(R.id.viewPager));
Any ideas?
You provide not that much information. But lets work with what we have.
I assume the Activity
has been started successfully and the DialogFragment
(that contains the ViewPager
) is displayed too:
The first line of your posted code onView(withId(R.id.viewPager)).check(matches(isDisplayed()));
does pass, because Espresso searches trough all views that have been added to the current window. And the dialog is somewhere in the window's layout hierarchy.
But assertNotNull(activity.findViewById(R.id.viewPager));
does fail, because the ViewPager
is not in the content view of the Activity
but in the FragmentDialog
.
You can verify view hierarchy with the help of the Hierarchy Viewer. Compare the hierarchy with and without the dialog.
Why do you want to add an IdlingResource
to the ViewPager
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With