1 minute read

Android Espresso Testing

In Part I we talked about how to setup Espresso testing framework, what is the activity testing rule and how to use uiautomatorviewer to help us find id of the view quickly.

In this Part II, we will write some tests against a simple TODO list application. Lets get started.

About the App under test

This is a very simple app with basically 2 screens. One to display a list of tasks:

and one for addiing new task or editing existing ones:

The test we are going to write is very straightforward, it tests that we are able to go to the new task screen, create and add a new task, and the new task is shown on the main screen.

Add the activity test rule

@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);

Write the test case

@Test
public void shouldBeAbleToAddTasksAndHaveThemVisible() {

    // go to new task screen
    onView(withId(R.id.menu_main_new_task)).perform(click());

    // enter task name
    onView(withId(R.id.new_task_task_name)).perform(click());
    onView(withId(R.id.new_task_task_name)).perform(typeText("foo"));

    // enter task desc
    onView(withId(R.id.new_task_task_desc)).perform(click());
    onView(withId(R.id.new_task_task_desc)).perform(typeText("bar"));

    // click add button
    Espresso.closeSoftKeyboard();
    onView(withId(R.id.action_button)).perform(click());

    // check the new task on screen
    onView(withText("foo")).check(matches(isDisplayed()));
}

All are pretty clear, notice that after entering some text, the softkeyboard may be blocking other views on the screen, so have to close it explicitly in the test.

Find the source code here.

~To be continued~

comments powered by Disqus