Example
/**
* @author piotrek1543
*
* This example provides a specific UI testing problem and how it is already solved
* with Google's Espresso. Notice that I used also Spoon framework, as Espresso
* lacks of taking screenshots functionality.
*/
@RunWith(AndroidJUnit4.class)
public class MainActivityAndroidTest {
@Rule
public ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void checkIfSettingsMenuItemsAreVisible() throws InterruptedException {
//open OptionsMenu to see available items
openActionBarOverflowOrOptionsMenu(mRule.getActivity());
//create a screenshot with 'options_menu' TAG
Spoon.screenshot(mRule.getActivity(), "options_menu");
//check if Settings item is Visible
onView(withText(R.string.action_settings)).check(matches(isDisplayed()));
//check if `Sort` item is Visible
onView(withText(R.string.action_sort)).check(matches(isDisplayed()));
//perform click on `Sort` OptionsMenu item
onView(withText(R.string.action_sort)).perform(click());
//create a screenshot with 'options_menu_sort' TAG
Spoon.screenshot(mRule.getActivity(), "options_menu_sort");
//check if `Sort -> By Value id` item is Visible
onView(withText(R.string.menu_sort_length)).check(matches(isDisplayed()));
//check if `Sort -> By Joke length` item is Visible
onView(withText(R.string.menu_sort_a_z)).check(matches(isDisplayed()));
}
}