Android-Espresso:使用R.color.xxx无法检查textView文本颜色

时间:2018-06-13 08:31:49

标签: android-espresso

这里是我的colors.xml

<color name="color_primary">#29c9b9</color>

这是我的xml

<TextView
            android:id="@+id/registerTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"           
            android:text="@string/register"
            android:textAllCaps="true"
            android:textColor="@color/color_primary"/>

所以我想在TextView android:textColor="@color/color_primary"

中编写Espresso测试检查

所以在这里我的测试:

    @Test
    public void registerTextViewTextColor() {
        onView(withId(R.id.registerTextView)).check(matches(withTextColor(Color.parseColor("#29c9b9"))));
}

这里是匹配者:

public static Matcher<View> withTextColor(final int expectedId) {
        return new BoundedMatcher<View, TextView>(TextView.class) {

            @Override
            protected boolean matchesSafely(TextView textView) {
                return expectedId == textView.getCurrentTextColor();
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
                description.appendValue(expectedId);
            }
        };
    }

测试工作正常。

所以我改变我的测试以使用 color_primary

@Test
public void registerTextViewTextColor() {
  onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)));}

但现在我收到了错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text color: <2131099686>' doesn't match the selected view.
Expected: with text color: <2131099686>
Got: "AppCompatTextView{id=2131296512, res-name=registerTextView, visibility=VISIBLE, width=180, height=53, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@9e165b3, tag=null, root-is-layout-requested=false, has-input-connection=false, x=451.0, y=1508.0, text=Register, input-type=0, ime-target=false, has-links=false}"

2 个答案:

答案 0 :(得分:3)

如果要从颜色资源中提取int,则视图匹配器应如下所示:

public static Matcher<View> withTextColor(final int expectedId) {
        return new BoundedMatcher<View, TextView>(TextView.class) {

            @Override
            protected boolean matchesSafely(TextView textView) {
                int colorId = ContextCompat.getColor(textView.getContext(), expectedId);
                return textView.getCurrentTextColor() == colorId;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
                description.appendValue(expectedId);
            }
        };
    }

然后这应该会产生预期的结果:

onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)))

答案 1 :(得分:0)

这对我来说是完美的,这对于检查文本颜色很有用。 onView(withText(“ Your text”))。check(matches(withTextColor(Color.parseColor(“#282828”))));

请注意#282828是正确的颜色代码。