Skip to main content

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.
onView(withText("XXYYZZ")).perform(click());
onView(withContentDescription("XXYYZZ")).perform(click()); 

 

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

 - matching a view which text starts with "XXYY" pattern:
onView(withText(startsWith("XXYY"))).perform(click());
 - matching a view which text ends with "YYZZ" pattern:
onView(withText(endsWith("YYZZ"))).perform(click());
 - assert that text of a particular view with specified R.id has content description which contains "YYZZ" string anywhere:
onView(withId(R.id.viewId)).check(matches(withContentDescription(containsString("YYZZ"))));
 - matching a view which text is equal to the specified string, ignoring case:
onView(withText(equalToIgnoringCase("xxYY"))).perform(click());
 - 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 R.id does not contain "YYZZ" string:
onView(withId(R.id.viewId)).check(matches(withText(not(containsString("YYZZ")))));

 

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

 - assert that text of a particular view with specified R.id doesn't start with "ZZ" string and contains "YYZZ" string anywhere:
onView(withId(R.id.viewId))
    .check(matches(allOf(withText(not(startsWith("ZZ"))), withText(containsString("YYZZ")))));
- assert that text of a particular view with specified R.id ends with "ZZ" string or contains "YYZZ" string anywhere:
onView(withId(R.id.viewId))
    .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 com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withContentDescription;
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!

Comments

Popular posts from this blog

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 g...

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...