How can we use espresso to test an android settings activity with an PreferenceFragment?

Viewed 2595
2 Answers

The Preferences are in a list so you have to query for the specific preference like this:

// Check if is displayed    
onData(allOf(is(instanceOf(Preference.class)), withKey("prefkey"))).check(matches(isDisplayed()));

// Perform click
onData(allOf(is(instanceOf(Preference.class)), withKey("prefkey"))).onChildView(withClassName(is(Switch.class.getName()))).perform(click());

I found this article helpful: http://www.winters.nz/2015/05/espresso-for-android-hints-properties.html

Related