Skip to main content

Posts

Showing posts from 2014

Espresso 2.0

It was almost a year since the previous Espresso release and finally Espresso 2.0 is released now. A good present before Christmas and New Year.

I'd like to avoid a deep dive in 2.0 features this time but focus on them rather in 2015 after having it a try.

For now take a look at Espresso Cheat Sheet - awesome overview of existing Espresso's features.

Enjoy your holidays and Happy New Year!

Catching CRASH or ANR of the Android app directly on your smartphone

In this post I just want to share with you my application which I'm using during daily testing activities and which helps me to have the CRASH stacktrace or ANR report immediately after it happens on your Android device - https://play.google.com/store/apps/details?id=com.error.hunter.

Please share your feedback regarding improvements if you'll have some :)

Update: I've uploaded this app source code to the github, so, you can also contribute if you want - https://github.com/denyszelenchuk/bug_radar.

Google Test Automation Conference - GTAC 2014

Hey. Don't miss GTAC 2014 https://developers.google.com/google-test-automation-conference/2014/ conference which is currently taking place in Kirkland.

Especially tomorrow talk from Espresso team. They plan to present something really big in regard of Android testing environment and also present the latest Espresso release.
Can't wait for it :)

You can watch live stream or view the recordings afterwards.

Discovering Espresso for Android: how to get current activity?

I see a lot of questions regarding getting current activity while testing with Espresso for Android across multiple activities. Below is the solution:
public Activity getActivityInstance(){ getInstrumentation().runOnMainSync(new Runnable() { public void run() { Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); for (Activity act: resumedActivities){ Log.d("Your current activity: ", act.getClass().getName()); currentActivity = act; break; } } }); return currentActivity; } The thing is that getActivitiesInStage(Stage.RESUMED) returns us all activities in RESUMED state, and the activity which is currently displayed on screen, will be the first one in list.

Update for Espresso 2.0 - you have to add below imports and slightly modify your method:
import static android.support.test.runner.lifec…

Discovering Espresso for Android: implementing CountingIdlingResource

Hi, after a long pause I’d like to post an example how to use Espresso’s CountingIdlingResource using lazy getter and setter pattern.

So, at first, what is the  CountingIdlingResource?

CountingIdlingResource - an implementation of IdlingResource that determines idleness by maintaining an internal counter. When the counter is 0 - it is considered to be idle, when it is non-zero it is not idle. This is very similar to the way a java.util.concurrent.Semaphore behaves.

 The counter may be incremented or decremented from any thread. If it reaches an illogical state (like counter less than zero) it will throw an IllegalStateException. This class can then be used to wrap up operations that while in progress should block tests from accessing the UI.

At second, why do I need it? Espresso developers claim that using their test framework you can “Leave your waits, syncs, sleeps, and polls behind and let Espresso gracefully manipulate and assert on the application UI when it is at rest.” This is t…

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) !…

Discovering Espresso for Android: creating custom Matchers.

Hi! This time I'll talk about custom Matchers that can be used with Espresso for Android (and not only). We'll go through steps how to create them and I'll provide you examples of already existing and very useful ones.

First of all a couple of words about Hamcrest library, which provides us with common matchers and possibility to create custom matchers, to pay tribute to it's authors.

From Humcrest project main page - Hamcrest provides a library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules.

In one of my previous post I've described already how to use some custom Hamcrest matchers. The base idea is that matcher is initialized with the expected values, which are compared against the actual object we are matching when invoking it.

Among of the common matchers you can create y…

Discovering Espresso for Android: swiping.

Hi, for today I have some tips about swiping ViewActions in Espresso for Android.

As you may know the latest Espresso release contains new swipeLeft and swipeRight ViewActions. They both are really useful when you'd like to swipe between activity fragments, tab layouts or any other UI elements.

You can use it as any other view action:
onView(withId(R.id.viewId)).perform(swipeRight());
But be aware that doing this you will operate on a view, in our case R.id.viewId, but not on the screen size. That means that to swipe right or left, for example between fragments you have to deal with some parent layout or maybe list view.

If you take a look inside Espresso's ViewActions.java class you will see below code for swipeRight() method:
  public static ViewAction swipeRight() {     return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.CENTER_LEFT,         GeneralLocation.CENTER_RIGHT, Press.FINGER);   }
As you may guess GeneralLocation.CENTER_LEFT and GeneralLocation.CENTER_RIGHT…

Espresso for Android 1.1 released!

Google Espresso Team just announced about the second 1.1 release of their small in size but huge in potential baby. The new features are:
so expected swiping ViewActions - swipeRight() and swipeLeft()maybe even more desired multi-window supportnew type text ViewAction - typeTextIntoFocusedView(String stringToBeTyped)bugfixes and improvements The release notes available at android-test-kit page - release notes Espresso 1.1.

I'd also like to mention useful features that are not present at release notes page:
added possibility to create custom Root matchersupdated scrollTo() ViewAction for horizontal scroll view supportadded NoActivityResumedException which indicates that there are no activities in stage RESUMEDadded DrawerActions and DrawerMatchersand last but not least - new stylish logo Stay tuned!

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…