How can we use espresso to test the activity of Android settings using PreferenceFragment?

Dear community,

How can we use espresso onView and execute in the “Settings” activity, which contains the PreferenceFragment as follows: http://developer.android.com/guide/topics/ui/settings.html#Fragment

+4
source share
2 answers

The settings are listed, so you need to request specific preferences as follows:

// 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 useful: http://www.winters.nz/2015/05/espresso-for-android-hints-properties.html

+1
source

// Check if it is displayed    
onData(PreferenceMatchers.withKey(context.getResources().getString(R.string.prefkey))).check(matches(isDisplayed()));
+1

All Articles