Last active
September 1, 2017 17:13
-
-
Save cpeppas/b5ffe6bd29b67d96416a to your computer and use it in GitHub Desktop.
Espresso CustomMatcher to help test things like Actionbar title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import static org.hamcrest.Matchers.is; | |
public class CustomMatchers { | |
public static Matcher<View> withResourceName(String resourceName) { | |
return withResourceName(is(resourceName)); | |
} | |
public static Matcher<View> withResourceName(final Matcher<String> resourceNameMatcher) { | |
return new TypeSafeMatcher<View>() { | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("with resource name: "); | |
resourceNameMatcher.describeTo(description); | |
} | |
@Override | |
public boolean matchesSafely(View view) { | |
int id = view.getId(); | |
return id != View.NO_ID && id != 0 && view.getResources() != null | |
&& resourceNameMatcher.matches(view.getResources().getResourceName(id)); | |
} | |
}; | |
} | |
} | |
Usage: | |
import static com.octo.android.sample.espresso.test.CustomMatchers.withResourceName; | |
...//Your TestEspressoClass | |
public void testActionBarTitleForScreenOneActivity() { | |
onView(allOf(isDescendantOfA(withResourceName("android:id/action_bar_container")), withText("My Activity"))) | |
.check(matches(isDisplayed())); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment