Android Automated UITesting : Faster and Simpler than before…!

Espresso is an API for UItesting of the android application developed by Google and it is indeed quite easy with fun attributes for developers to write reliable UI tests.

The Espresso API encourages test authors to think in terms of what a user might do while interacting with the application – locating UI elements and interacting with them. At the same time, the framework prevents direct access to activities and views of the application because holding on to these objects and operating on them off the UI thread is a major source of test flakiness. Thus, you will not see methods like getView and getCurrentActivity in the Espresso API. You can still safely operate on views by implementing your own ViewActions and ViewAssertions.

Important components of Espresso:

  • Espresso – Entry point to interactions with views (via onView and onData). Also exposes APIs that are not necessarily tied to any view (e.g. pressBack).
  • ViewMatchers – A collection of objects that implement Matcher<? super View> interface. You can pass one or more of these to the onView method to locate a view within the current view hierarchy.
  • ViewActions – A collection of ViewAction that can be passed to the ViewInteraction.perform method (for example, click).
  • ViewAssertions– A collection of ViewAssertions that can be passed the ViewInteraction.check method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.

How to setup in the android application:

  1. In your build.gradle file, add the following dependency:
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
  2.  Add to the same build.gradle file the following line in android.defaultConfig
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

How to write Espresso UITesting TestCases :

Android Studio creates tests by default in src/androidTest/java/com.example.package/
Example test:

@LargeTest
public class LoginEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
  public HelloWorldEspressoTest() {
      super(MainActivity.class);
 @Override
    public void setUp() throws Exception {
        super.setUp();
        getActivity();
    }
public void testLoginAction () {

                 Espresso.onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources().getIdentifier("com.pragmaapps.expressotest:id/usernameET", null, null))).perform((typeText("username")), closeSoftKeyboard());

   try {
          Thread.sleep(1000);
   } catch (InterruptedException e) {
                e.printStackTrace();
       }
            Espresso.onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.pragmaapps.expressotest:id/passwordET", null, null))).perform((typeText("test")), closeSoftKeyboard());
  try {
       Thread.sleep(1000);
  } catch (InterruptedException e) {
                e.printStackTrace();
     }

            Espresso.onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.pragmaapps.expressotest:id/loginBtn", null,   null))).perform(click());
 }

}

How to create a test configuration:

In Android Studio:

  • Open Run menu | Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:
    android.support.test.runner.AndroidJUnitRunner

Espresso has been adopted by hundreds of projects. With the latest 2.0 release, Espresso is now part of the Android Support Repository. Espresso’s test run is in sync with the UI thread and does not rely on sleep/poll mechanisms but is rather event driven.Espresso seems to shine when needing to do neat little test runs per activity.
You can also try Espresso on real devices at Testdroid Cloud.

Sample Source Code

For complete source code, click here

References

Android-Test-Kit

Espresso

Leave a Comment

Scroll to Top