Friday, January 31, 2014

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:


But be aware that doing this you will operate on a view, in our case, 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 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 are the from and to coordinates accordingly inside the view. The full positions list which can be used as from and to coordinates are: TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT, CENTER, CENTER_RIGHT, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT.

For other two parameters' names Swipe.FAST and Press.FINGER it's clear without saying what each of them means. For now Swipe has only FAST (100 milliseconds) and SLOW (1500 milliseconds) swipe speeds, and Press has PINPOINT (1x1 mm), FINGER (16x16 mm) and THUMB (25x25 mm) press areas.

Of course there is always an option to implement your own speed and press area - thanks for Espresso team for making the source code open.

Seems to be clear enough, right?

And the last thing - implementing our own swipe down and up ViewActions which can be used to initiate swipe down to refresh action or to perform swipe up (going to the bottom of the view), since they are not yet included into Espresso's code:

public static ViewAction swipeDown() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
        GeneralLocation.BOTTOM_CENTER, Press.FINGER);

public static ViewAction swipeUp() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,
        GeneralLocation.TOP_CENTER, Press.FINGER);

Thanks for reading!

Friday, January 10, 2014

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:
  1. so expected swiping ViewActions - swipeRight() and swipeLeft()
  2. maybe even more desired multi-window support
  3. new type text ViewAction - typeTextIntoFocusedView(String stringToBeTyped)
  4. 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:
  1. added possibility to create custom Root matchers
  2. updated scrollTo() ViewAction for horizontal scroll view support
  3. added NoActivityResumedException which indicates that there are no activities in stage RESUMED
  4. added DrawerActions and DrawerMatchers
  5. and last but not least - new stylish logo
Stay tuned!

Monday, January 6, 2014

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 expectedText or specified expectedContentDescriptionText parameter passed into it.


Using Hamcrest String matchers we can create more flexible matcher combinations:

 - matching a view which text starts with "XXYY" pattern:
 - matching a view which text ends with "YYZZ" pattern:
 - assert that text of a particular view with specified has content description which contains "YYZZ" string anywhere:
 - matching a view which text is equal to the specified string, ignoring case:
 - matching a view which text is equal to the specified text when whitespace differences are (mostly) ignored:
onView(withText(equalToIgnoringWhiteSpace("XX YY ZZ"))).perform(click());
 - assert that text of a particular view with specified does not contain "YYZZ" string:


 Adding allOf() or anyOf() Hamcrest Core matchers gives us even more power:

 - assert that text of a particular view with specified doesn't start with "ZZ" string and contains "YYZZ" string anywhere:
    .check(matches(allOf(withText(not(startsWith("ZZ"))), withText(containsString("YYZZ")))));
- assert that text of a particular view with specified ends with "ZZ" string or contains "YYZZ" string anywhere:
    .check(matches(anyOf(withText(endsWith("ZZ")), withText(containsString("YYZZ")))));
The last note - to use all of mentioned above ViewAssertions, ViewMatchers and Hamcrest matchers just add below imports:
import static;
import static;
import static;
import static;
import static;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
So, basically it. If you stuck at some point and don't have an idea how to proceed, you can ask a question in androit-test-kit Google group. You'll probably get an answer from co-author of Espresso tool - +Valera Zakharov or other experienced users.

Thanks for reading!