Code snippets in Espresso

A few snippets from Espresso — the automated testing framework for Android

Espresso Snippets

The Espresso framework is extremely powerful but can be quite complex.

Spinners

Selecting an item from a spinner is surprisingly easy. It’s simply a case of getting your spinner view (ViewInteraction), performing a click and then leveraging the onData functionality.

ViewInteraction spinner1 = onView(allOf(withId(R.id.spinner_1),                    withParent(withId(R.id.linear_spinner_parent_1)))); spinner1.perform(scrollTo(), click()); onData(hasToString(stringYouWantToSelect1)).perform(click()); ViewInteraction spinner2 = onView(allOf(withId(R.id.spinner_2),                    withParent(withId(R.id.linear_spinner_parent_2)))); spinner2.perform(scrollTo(), click()); onData(hasToString(stringYouWantToSelect2)).perform(click());

Starting new Activity

Checking a new activity has started can be a vital part of your testing strategy and can be done by creating an ActivityMonitor and then waitForActivityWithTimeout(). Then all that’s left is to assert that am.getHits() equals the number of times your activity should launch i.e. 1.

Instrumentation.ActivityMonitor am = getInstrumentation().addMonitor( NextActivity.class.getName(), null, true); ViewInteraction button = onView(withId(R.id.button_next)); button.perform(scrollTo(), click()); am.waitForActivityWithTimeout(3000); assertEquals(1, am.getHits());
public static Activity getActivity() {        final Activity[] currentActivity = new Activity[1];        onView(allOf(withId(android.R.id.content), isDisplayed())).perform(new ViewAction() {            @Override            public Matcher getConstraints() {                return isAssignableFrom(View.class);            }            @Override            public String getDescription() {                return "Getting activity";            }            @Override            public void perform(UiController uiController, View view) {                if (view.getContext() instanceof Activity) {                    Activity activity1 = ((Activity) view.getContext());                    currentActivity[0] = activity1;                }            }        });        return currentActivity[0];    }

Idling Resources

I have used a couple of different idling resources which are used to effectively wait for something to happen during a test. Hopefully these will inspire you to write your own.

public class TextViewHasTextIdlingResource implements IdlingResource {    private final TextView mView;    private final boolean mHasText;    private boolean mIdle;    private ResourceCallback mResourceCallback;    public TextViewHasTextIdlingResource(final TextView view, boolean hasText) {        this.mView = view;        this.mHasText = hasText;        this.mIdle = false;        this.mResourceCallback = null;    }    @Override    public final String getName() {        return TextViewHasTextIdlingResource.class.getSimpleName();    }    @Override    public final boolean isIdleNow() {        mIdle = mIdle || (mHasText != Strings.isNullOrEmpty(mView.getText().toString()));        if (mIdle && mResourceCallback != null) {            mResourceCallback.onTransitionToIdle();        }        return mIdle;    }    @Override    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {        mResourceCallback = resourceCallback;    } }
public class ViewVisibilityIdlingResource implements IdlingResource {    private final View mView;    private final int mExpectedVisibility;    private boolean mIdle;    private IdlingResource.ResourceCallback mResourceCallback;    public ViewVisibilityIdlingResource(final View view, int expectedVisibility) {        this.mView = view;        this.mExpectedVisibility = expectedVisibility;        this.mIdle = false;        this.mResourceCallback = null;    }    @Override    public final String getName() {        return ViewVisibilityIdlingResource.class.getSimpleName();    }    @Override    public final boolean isIdleNow() {        mIdle = mIdle || mView.getVisibility() == mExpectedVisibility;        if (mIdle && mResourceCallback != null) {            mResourceCallback.onTransitionToIdle();        }        return mIdle;    }    @Override    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {        mResourceCallback = resourceCallback;    } }
public class ViewHasChildrenIdlingResource implements IdlingResource {    private final ViewGroup mView;    private final int mExpectedNoOfChildren;    private boolean mIdle;    private ResourceCallback mResourceCallback;    public ViewHasChildrenIdlingResource(final ViewGroup view, int noOfChildren) {        this.mView = view;        this.mExpectedNoOfChildren = noOfChildren;        this.mIdle = false;        this.mResourceCallback = null;    }    @Override    public final String getName() {        return ViewHasChildrenIdlingResource.class.getSimpleName();    }    @Override    public final boolean isIdleNow() {        mIdle = mIdle || mView.getChildCount() >= mExpectedNoOfChildren;        if (mIdle && mResourceCallback != null) {            mResourceCallback.onTransitionToIdle();        }        return mIdle;    }    @Override    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {        mResourceCallback = resourceCallback;    } }

TextView Url

To test clicking on a url within a textview tries to open that url you can do the following:

ViewInteraction textView = onView(                allOf(withId(R.id.text_info),                        withParent(withId(R.id.relative_main)))); textView.perform(ViewActions.openLinkWithText("terms and conditions")); intended(allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse("http://www.google.co.uk"))));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store