Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can to test by Espresso android.widget.TextView setError?

I have password.setError(getResources().getString(R.string.incorrect_data)); If I set invalid password - show textView with text "Invalid data!", I need to test it by Espresso, I write:

onView(withText(R.string.incorrect_data)).check(matches(isDisplayed()));

But it is wrong, I have:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131493034>[incorrect_data] value: Invalid data!
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

And If I write: onView(withText("Invalid data!")).check(matches(isDisplayed()));

I have:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Invalid data!"
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

I use Espresso 2:

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.Espresso.onView;

Help me, please.

like image 490
Paushchyk Julia Avatar asked Feb 09 '15 10:02

Paushchyk Julia


3 Answers

helped me:

onView(withId(R.id.password)).check(matches(withError(
                getActivity().getString(R.string.incorrect_data))));

private static Matcher<View> withError(final String expected) {
        return new TypeSafeMatcher<View>() {

            @Override
            public boolean matchesSafely(View view) {
                if (!(view instanceof EditText)) {
                    return false;
                }
                EditText editText = (EditText) view;
                return editText.getError().toString().equals(expected);
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }
like image 192
Paushchyk Julia Avatar answered Sep 28 '22 08:09

Paushchyk Julia


I wrote a custom matcher for Espresso 1.x which compares the textColor of a TextView against a given value. Maybe you could adopt this solution to EditText.getError(...) for Espresso 2.x.

/**
 * Returns a matcher that matches {@link TextView}s based on text property value. Note: View's
 * text property is never null. If you setText(null) it will still be "". Do not use null
 * matcher.
 *
 * @param integerMatcher {@link Matcher} of {@link String} with text to match
 */
public static Matcher<View> withCurrentTextColor(final Matcher<Integer> integerMatcher) {
  checkNotNull(integerMatcher);
  return new BoundedMatcher<View, TextView>(TextView.class) {
     @Override
     public void describeTo(Description description) {
        description.appendText("with text color: ");
        integerMatcher.describeTo(description);
     }

     @Override
     public boolean matchesSafely(TextView textView) {
        return integerMatcher.matches(textView.getCurrentTextColor());
     }
  };
}

/**
 * Returns a matcher that matches {@link TextView} based on it's text property value. Note:
 * View's Sugar for withTextColor(is("string")).
 */
public static Matcher<View> withCurrentTextColor(int color) {
  return withCurrentTextColor(is(color));
}

And then in your test case:

onView(withId(R.id.text_warning_title)).check(matches(withCurrentTextColor(activity.getResources().getColor(R.color.black_light))));
like image 28
Christopher Avatar answered Sep 28 '22 08:09

Christopher


Took me awhile, but here's the Kotlin version of Paushchyk Julia's java code.

    onView(withId(R.id.usernameText)).check(matches(withError(mActivity!!.getString(R.string.error_invalid_email))))


private fun withError(expected: String): Matcher<View> {
          return object : TypeSafeMatcher<View>() {
              override fun describeTo(description: org.hamcrest.Description?) {
              }

              override fun matchesSafely(item: View?): Boolean {
                  return if (item !is EditText) {
                      false
                  } else item.error.toString() == expected
              }
          }
      }
like image 37
clinton wong Avatar answered Sep 28 '22 07:09

clinton wong