4

I'm trying to build a test that validates my PreferenceScreen view with Espresso. The text that I want to click on is at the bottom. So how do I scroll down in a PreferenceScreen with Espresso ?

I tried this code, but it did not work:

onData(allOf(is(instanceOf(String.class)), is("Notifications"))).perform(click());

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
benoma777
  • 75
  • 1
  • 6
  • I have not done it but I think you should use PreferenceMatchers : https://developer.android.com/reference/android/support/test/espresso/matcher/PreferenceMatchers.html – jeprubio Jul 01 '16 at 13:56

1 Answers1

6

This used to work:

onData(PreferenceMatchers.withTitleText("Notifications")).perform(click());

But nowadays you can use RecyclerViewActions:

onView(withId(androidx.preference.R.id.recycler_view))
    .perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("Notifications")), 
       click()));
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • 1
    Tried this solution; my view is completely visible, but it throws `No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView` – Farid Mar 13 '18 at 07:07
  • 1
    Hi, I tried using PreferenceMatchers, but I wasn't able to run it. I always encountered android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView. – jjz Jun 26 '18 at 06:10
  • @jzarsuelo: The PreferenceMatchers do not work with Android X and support library. Try using RecyclerViewActions. See this answer and my comment below it: https://stackoverflow.com/a/52144554/2380518 – Miloš Černilovský Dec 17 '18 at 11:59