Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can we use espresso onView and perform in a Settings activity that contains a PreferenceFragment like this: http://developer.android.com/guide/topics/ui/settings.html#Fragment

like image 573
Jérôme Avatar asked Sep 25 '22 04:09

Jérôme


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

like image 108
subathra Avatar answered Nov 26 '22 23:11

subathra


Try this below logic

// Check if it is displayed    
onData(PreferenceMatchers.withKey(context.getResources().getString(R.string.prefkey))).check(matches(isDisplayed()));
like image 35
vibroto Avatar answered Nov 26 '22 22:11

vibroto