How to run Espresso tests in Testdroid Cloud

Bitbar, the mobile devops company. Logo, large

Dear Testdroiders,

A couple of months back Google open sourced their latest Android Test automation framework – called Espresso. Since, there has been noticeable amount of excitement around this new framework that was initially introduced already at Google Test Automation Conference 2013 in New York at the end of April.

We here at Testdroid are naturally very excited about this newcomer and wanted to give it an instant try at our Testdroid Cloud. You should definitely try it too – and let us know how well this variant of Espresso tastes to you.

Here is a video of the introduction:

The video from Google Test Automation Conference – GTAC 2013.
And, the Google Espresso was finally published as an open source in last October as part of Android Test Kit. More information about it can be found here: https://code.google.com/p/android-test-kit/.

INTRODUCTION

The Google Espresso framework is a thin abstraction layer (600 lines of code) on top of Android Instrumentation Framework and as such is easy to deploy on any Android device (Espresso currently supports API levels 8, 10, 15-18). One of the great benefits of Espresso framework is that it synchronizes with the Android UI thread which makes user specified sleeps unnecessary – the framework will know when each UI action is executed (as UI thread is on idle) and will execute the test commands automatically at the right time making test scripts more robust and extremely simple.

Another notable characteristic of Espresso framework is its use of Hamcrest matchers (https://code.google.com/p/hamcrest/) which makes the test scripts very simple to write and read. In its simplicity Espresso has only three kinds of actions: matchers (for finding views and elements), actions (for clicking etc) and checks (for assertions/validations).

We prepared a short code sample using our already classic Twitter app example:

public void testEspresso() {

        // Check if view with the text ‘Hello.’ is shown

        onView(withText(“Hello.”)).check(matches(isDisplayed()));

        // R class ID identifier for ‘Sign in’ – and click it

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/sign_in”, null, null)))

                .perform(click());

        // R class ID identifier for entering username

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/login_username”, null, null)))

                .perform((typeText(“username”)));

        // R class ID identifier for entering password

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/login_password”, null, null)))

                .perform((typeText(“password”)));

        // R class ID identifier for clicking log in

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/login_login”, null, null)))

                .perform(click());

        // Activate the text field to compose a tweet

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/menu_compose_tweet”, null, null)))

                .perform(click());

        // Type the tweet

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/edit”, null, null)))

                .perform((typeText(“Testdroid is awesome!”)));

        // Tweeting!

        onView(withId(getInstrumentation().getTargetContext().getResources()

                .getIdentifier(“com.twitter.android:id/composer_post”, null, null)))

                .perform(click());

}

The end-result is easy-to-write, fast and robust tests and apparently over 50 different Google apps are tested using this framework so guys at Google are really eating their own dog food 😉

HOW TO START USING IT?

So, how you can run tests written with this cool new framework in Testdroid Cloud? Just few steps and you’ll get your app tested with this framework on hundreds of real Android devices:

Step #1:

First, you have to specify instrumentation test runner in test project:

<instrumentation
android:name=”com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner”
android:targetPackage=”$YOUR_TARGET_APP_PACKAGE”/>

Step #2:

Secondly, you need to include espresso library to your libraries. We recommend to use Espresso as a standalone library: espresso-1.1-bundled.jar. The best way to include it into test project is putting it into libs directory.

Step #3:

Once you have configured the test project you can build it and upload into Testdroid Cloud. Before you run this test you have to set Custom Instrumentation Test Runner, as shown on screenshot below:

espresso

The text that you need to configure will be done in Advanced configuration in Test run creation view. Click Test execution tab and select Test runner section. The following test runner should be placed here:

com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner

Finally you are good to go and you can see the results of your Espresso tests in minutes – thanks to the blazing fast parallel test execution of Testdroid Cloud and the speed of Espresso framework.

Happy testing! 🙂

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close