如何通过 Espresso android.widget.TextView setError 进行测试?

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

我有password.setError(getResources().getString(R.string.incorrect_data)); 如果我设置了无效密码 - 显示带有文本 "Invalid data!" 的 textView, 我需要用 Espresso 测试一下,我写道:

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

但是错了,我有:

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}

如果我写:onView(withText("Invalid data!")).check(matches(isDisplayed()));

我有:

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}

我用 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;

请帮帮我。

我为 Espresso 编写了一个自定义匹配器 1.x,它将 TextView 的 textColor 与给定值进行比较。也许您可以将此解决方案用于 EditText.getError(...) 的 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));
}

然后在你的测试用例中:

onView(withId(R.id.text_warning_title)).check(matches(withCurrentTextColor(activity.getResources().getColor(R.color.black_light))));

帮助了我:

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) {

            }
        };
    }

Espresso 分三步工作 1:找到视图 2:对视图执行操作 将触发您要验证的结果的操作 3:Check 如果视图执行它的操作。 使用提供的一些断言验证操作或结果

onView(withId(R.id.password_text)).perform(action);
onView(withId(R.id.view_for_expected_outcome)).check(expected outcome)

浏览 https://google.github.io/android-testing-support-library/docs/index.html 以了解有关 Espresso 的更多信息。

花了我一段时间,但这是 Paushchyk Julia 的 java 代码的 Kotlin 版本。

    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
              }
          }
      }