Descriptions for ViewAssertion in Espresso

espresso allof
espresso ondata
espresso onview
espresso cheat sheet
espresso matchers
espresso custom matcher
espresso check if view exists
androidx espresso

I have this code:


How can I add my own message to it in case of a fall of the test with an error? For example as in junit:

assertEquals("My message", expected, actual)

You could write your own custom matcher. The following code will check the hint in a EditText. Check out this example:

private Matcher<View> withHint(final Matcher<String> stringMatcher) {
    return new BoundedMatcher<View, EditText>(EditText.class) {

        public boolean matchesSafely(EditText view) {
            final CharSequence hint = view.getHint();
            return hint != null && stringMatcher.matches(hint.toString());

        public void describeTo(Description description) {
            description.appendText("YOUR WHATEVER CUSTOM MESSAGE");


Espresso basics, The Espresso API encourages test authors to think in terms of what a user might ViewAssertions – A collection of ViewAssertion objects that can be with should either contain descriptive text or have a content description. ViewAssertion selectedDescendantsMatch (Matcher<View> selector, Matcher<View> matcher) Returns a generic ViewAssertion that asserts that the descendant views selected by the selector match the specified matcher. Example: onView(rootView).check(selectedDescendantsMatch(not(isAssignableFrom(TextView.class)), hasContentDescription()));

You can make your own custom method like isVisible(int id) whose implementation would be something like this:

public boolean isVisible(int elementId) {
 try {
  return true;
} catch(Throwable t) {
   return false;

And your assertion in your test would look something like this:

assertEquals("My failure message", expected, isVisible(;

Espresso recipes, Luckily, Espresso handles that for us. view matcher into a ViewAssertion by using ViewAssertions.matches() . describeTo(description) Assertions belong in ViewAssertion classes. View action code will executed on the UI thread, therefore you should not block, perform sleeps, or perform other expensive computations. The test framework will wait for the UI thread to be idle both before and after perform () is called.

Another option is to use Espresso .withFailureHandler() and writing a generic failure handler that will make the test fail using the fail(String; Method.

More this failure handler here:

Descriptions for ViewAssertion in Espresso, Descriptions for ViewAssertion in Espresso - android. toString()); } #Override public void describeTo(Description description) { description.appendText("YOUR​  Custom RecyclerView Matcher and ViewAssertion with Espresso (Kotlin) Instrumentation test is important to make sure your app shows the correct UI as expected. It also prevents any break of logic while you’re refactoring. Espresso is a default library for Android instrumentation test.

Custom RecyclerView Matcher and ViewAssertion with Espresso , Custom RecyclerView Matcher and ViewAssertion with Espresso (Kotlin) describeTo allows us to append our own description to the custom matcher. Espresso has many ViewMatcher options which are very effective in uniquely locate UI element. You can also combine and create a combination of View Matchers to find element uniquely. In Espresso we can locate the lement very effectively even though with help of these View Matchers.

Java Code Examples , Java Code Examples for getName())); onView(withId( ViewAssertion() { @Override getCount(); return true; } @Override public void describeTo(Description description)  ViewAssertion isLeftAlignedWith (Matcher<View> matcher) Returns a ViewAssertion that asserts that view displayed is left aligned with the view matching the given matcher. The left 'x' coordinate of the view displayed must equal the left 'x' coordinate of the view matching the given matcher.

espresso/core/src/main/java/android/support/test/espresso/assertion , import static Returns a generic {@link ViewAssertion} that asserts that a view exists in the describeTo(description);. Testing is essential to delivering high-quality software. Espresso is the tool of choice for many Android developers, who need to test the logic and UI of their applications. Join instructor Chiu-Ki Chan to learn the essentials of Android Espresso for UI testing.