Skip to main content

Tired of testing - take some Espresso!


Recently Google announced the new testing tool Espresso for Android - a simple API for writing reliable UI tests. So, I'd like to share with you some information about it and what I've experienced using it so far. 

At first Espresso tool was not announced alone, it is used together with GoogleInstrumentationTestRunner - an improved InstrumentationTestRunner. That means that you have to claim it's usage in AndroidManifest.xml file of your test application and set it in test project Run Configurations.  

Espresso set up instructions are quite clear, at least for Eclipse IDE. If everything goes well you'll manage to run first test in 30 minutes. 

Keep in mind following points which I hope will help you to save some time before starting:
  • you have to check that your test project and project under test have no reused libraries. Most common one is android-support-v4.jar. You have to remove them from test project. Having jars in both projects will lead to below issue
    android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests
  • set up to every test in Eclipse Run->Run Configurations with GoogleInstrumentationTestRunner if you'd like to run concrete test from your project.
  • there is no support of testing navigation drawer element yet but under this link you can find a workaround. Seems that official support of this feature will be added soon.
  • if the element you want to perform action on is not visible on device screen - you have to scroll to it first or, in case of lists, use onData() method.
What I personally like in Espresso:
  1. fast tests execution. Comparing to Robotium, Espresso's test are running much more faster (up to 3-4 times).
  2. you don't need to care about waitForActivity anymore, Espresso will handle this for you.
  3. responsive support - you may ask question in Google group - android-test-kit.
  4. clear test commands.
  5. you can run your tests from command line which allows you to integrate them into CI process.
  6. possibility to write multiply instructions for one element in one command:
onView(withId(is(R.id.edit_password))).perform(
    scrollTo(),
    click(),
    typeText(password),
    closeSoftKeyboard());
What I don't like:
  1. (and like the same time) to fully use the power of onData() command you must implement matchers by yourself, but when you got how to do it they become the real powerful tool for you.
  2. you have to handle delayed response from server request before continuing with a test - example is here.
  3. lack of documentation that covers all Espresso's features.
Summarizing all above I'd like to say that at first glance it seems that Espresso's API is clear and understandable but after some time you understand that it requires deeper development knowledge. Seems that Espresso is targeting more developers audience than testers.

And taking into consideration that Google takes care of it, this tool probably will grow as a snow ball in the nearest feature. So, keep monitoring ;)


Popular posts from this blog

Espresso & UIAutomator - the perfect tandem

Espresso for Android is perfect and fast test automation framework, but it has one important limitation - you are allowed to operate only inside your app under test context.

That means that it is not possible to automate tests for such app features like:

application push notificationscontact synchronizationnavigating from another app to your app under test,
since you have to deal with other apps from the mobile device - Notification Bar, Contacts or People app, etc. 
In fact it wasn't possible until the release of UIAutomator 2.0. As stated in Android Developers blog post - "...Most importantly, UI Automator is now based on Android Instrumentation...".  And because of that we can run UIAutomator tests as well as Espresso tests using Instrumentation test runner.

In addition to that we can combine UIAutomator tests together with Espresso tests and this gives us the real power and control over the phone and application under test.

In the below example I'll explain  how …

Discovering Espresso for Android: matching and asserting view with text.

After more than a month of using great test tool from Google - Espresso for Android, I'd like to share with you some of my experience. I assume that you've already added espresso jar into your project, spent some time playing with Espresso samples and have basic understanding how this tool works.

In this post I'll show how to match particular view with text or assert that it contains (or not) specified Strings. Before we start, you have to take a look at Hamcrest matchers - Hamcrest tutorial and API Reference Documentation, which are used together with Espresso's ViewAssertions and ViewMatchers and included into Espresso standalone library. Pay more attention to Matcher<java.lang.String> matchers.

So, here we go. For simplicity following String "XXYYZZ" will be used as a expected text pattern.
Espresso ViewMatchers class implements two String matcher methods withText() and withContentDescription() which will match a view which text is equal to specified…

Testing that Android AlarmManager has an alarm set.

Just a small post from my recent experience - how to test that AlarmManager has an alarm set.

The first approach is to do it programmatically - let's assume we registered our alarm as below:
Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.MINUTE, 1); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent);  And now to check that registered above alarm is active we have to do the following:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0, new Intent("com.my.package.MY_UNIQUE_ACTION"), PendingIntent.FLAG_NO_CREATE) !…