Android App UI testing with Espresso

Getting bored to run the same UI flow again and again for testing? Try using Espresso.

Image result for UI testing image android espresso

Before Started

Unit Tests

Instrumentation Tests

Espresso Basics

1. ViewMatchers — allows to find view in the current view hierarchy. Simply it refers to "find something".2. ViewActions — allows to perform actions on the views. Simply it refers to "do something".3. ViewAssertions — allows to assert state of a view. Simply it refers to "check something".
onView(ViewMatcher)          -- finds the view
.perform(ViewAction) -- perform an action on the view
.check(ViewAssertion); -- validates an assertion

Make Espresso Ready in your App

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Some Annotations Used in Espresso

@BeforeClass:

@Rule:

@Before:

@Test:

@After:

@AfterClass:

Note:You can have more than one method annotated with any of the annotations like @Before,@Beforeclass etc. But the order in which JUnit finds methods is not guaranteed.Like if there is two methods annotated with @Before, There is no guarantee which one will be executed.

Now it is time for Coding

module-name/src/androidTest/java/
    String userName = "shishir13";
String password = "password";

onView(withId(R.id.userNameEt))
.perform(typeText(password),closeSoftKeyboard());

onView(withId(R.id.passwordEt))
.perform(typeText(password),closeSoftKeyboard());

onView(withId(R.id.loginButton))
.perform(click());
onView(withId(R.id.login_success_tv))
.check(matches(withText("login_success")));
    String userName = "shishir13";
String userEmail = "shiktoshishir15@gamil.com"
String password = "password";
String confirmPassword = "password";
onView(withId(R.id.userNameEt))
.perform(typeText(userName),closeSoftKeyboard());
onView(withId(R.id.userEmailEt))
.perform(typeText(userEmail),closeSoftKeyboard());
onView(withId(R.id.passwordEt))
.perform(typeText(password),closeSoftKeyboard());
onView(withId(R.id.passwordEt))
.perform(typeText(confirmPassword),closeSoftKeyboard());
onView(withId(R.id.signUpButton))
.perform(click());
onView(withId(R.id.sign_up_success_tv))
.check(matches(withText("signup_success")));
    onView(withId(R.id.userNameEt))
.perform(typeText("Shishir_13"),closeSoftKeyboard());

onView(withId(R.id.goToNextActivity))
.perform(click());
onView(withId(R.id.greeting_message_tv))
.check(matches(withText("Welcome Shishir_13")));
    onView(withId(R.id.spinnerID))
.perform(click()); // Perform click on spinner
onData(allOf(is(instanceOf(String.class)),
is(COUNTRY).perform(click());
onView(withId(R.id.selected_country))
.check(matches(withText("selected" + COUNTRY)));
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
     onView(withText("AutoCompleteText"))

.inRoot(withDecorView(not(is(getActvity().getWindow().getDecorView()))))
.check(matches(isDesplayed()));

To Avoid Exceptions !!

{ 'designation' : 'Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }