Android tests build error: Multiple dex files define Landroid/support/test/BuildConfig

Update (9/07/2015): You can continue to work with 22.2.1 if you use the following excludes: androidTestCompile (‘com.android.support.test.espresso:espresso-core:2.2’) { exclude group: ‘com.android.support’, module: ‘support-annotations’ } androidTestCompile (‘com.android.support.test:runner:0.3’) { exclude group: ‘com.android.support’, module: ‘support-annotations’ } androidTestCompile (‘com.android.support.test:rules:0.3’) { exclude group: ‘com.android.support’, module: ‘support-annotations’ } If you depend on espresso-contrib, you need the exclude as well. Update (8/03/2015): … Read more

Testing background color espresso Android

In my tests I have the following matcher for testing EditText color: public static Matcher<View> withTextColor(final int color) { Checks.checkNotNull(color); return new BoundedMatcher<View, EditText>(EditText.class) { @Override public boolean matchesSafely(EditText warning) { return color == warning.getCurrentTextColor(); } @Override public void describeTo(Description description) { description.appendText(“with text color: “); } }; } And usage is : onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));

Click by bounds / coordinates

Espresso has the GeneralClickAction, this is the underlying implementation of ViewActions click(), doubleClick(), and longClick(). The GeneralClickAction‘s constructor takes a CoordinatesProvider as second argument. So the basic idea is to create a static ViewAction getter which provides a custom CoordinatesProvider. Something like this: public static ViewAction clickXY(final int x, final int y){ return new GeneralClickAction( … Read more

Using Espresso to click view inside RecyclerView item

You can do it with customize view action. public class MyViewAction { public static ViewAction clickChildViewWithId(final int id) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return null; } @Override public String getDescription() { return “Click on a child view with specified id.”; } @Override public void perform(UiController uiController, View view) { View … Read more

Using Espresso to Unit Test Google Maps

Short Answer: With espresso is not really possible. A solution might be to use UIAutomator: https://developer.android.com/tools/testing-support-library/index.html#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html So you need to: 1) add gradle dependencies: dependencies { androidTestCompile ‘com.android.support.test:runner:0.2’ androidTestCompile ‘com.android.support.test:rules:0.2’ androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.1′ } 2) make sure you add at least title to your markers even if you’re not using it. 3) Write the test, … Read more

How to assert inside a RecyclerView in Espresso?

Pretty easy. No extra library is needed. Do: onView(withId(R.id.recycler_view)) .check(matches(atPosition(0, withText(“Test Text”)))); if your ViewHolder uses ViewGroup, wrap withText() with a hasDescendant() like: onView(withId(R.id.recycler_view)) .check(matches(atPosition(0, hasDescendant(withText(“Test Text”))))); with method you may put into your Utils class. public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public … Read more

Thread.sleep( ) with Espresso

On my mind correct approach will be: /** Perform action of waiting for a specific view id. */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return “wait for a specific view with id <” + … Read more

ProgressBars and Espresso

If the ProgressBar is invisible when the test starts, the Drawable can be replaced with by a custom ViewAction: // Replace the drawable with a static color onView(isAssignableFrom(ProgressBar.class)).perform(replaceProgressBarDrawable()); // Click a button (that will make the ProgressBar visible) onView(withText(“Show ProgressBar”).perform(click()); The custom ViewAction: public static ViewAction replaceProgressBarDrawable() { return actionWithAssertions(new ViewAction() { @Override public Matcher<View> … Read more