Issue
I am trying to verify color of text view in android app.
healthHistoryPage.allergies.check(matches(hasTextColor(android.R.color.black))))
I am getting an error saying:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has color with ID 17170444' doesn't match the selected view.
Expected: has color with ID android:color/black
But I dont see the whats the actual color value anywhere in error message. Is there a way I can fetch the actual color value if I dont have access to source code.
Solution
See here the adjusted hasTextColor matcher of the original: https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
public static Matcher<View> hasTextColor(final int colorResId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
private Context context;
@Override
protected boolean matchesSafely(TextView textView) {
context = textView.getContext();
int textViewColor = textView.getCurrentTextColor();
int expectedColor;
if (Build.VERSION.SDK_INT <= 22) {
expectedColor = context.getResources().getColor(colorResId);
} else {
expectedColor = context.getColor(colorResId);
}
return textViewColor == expectedColor;
}
@Override
public void describeTo(Description description) {
String colorId = String.valueOf(colorResId);
String colorCode = "";
if (context != null) {
colorId = context.getResources().getResourceName(colorResId);
//added get color hex code
@ColorInt int colorInt = context.getResources().getColor(colorResId);
colorCode = String.format("#%06X", (0xFFFFFF & colorInt));
}
description.appendText("has color with ID " + colorId);
//added output color code in error msg
description.appendText(" and color code: " +
colorCode);
}
};
}
Note: the //added
comments with the modifications for the color hex code
This will log an error similar to:
... Expected: has color with ID com.appham.sandbox:color/colorAccent and color code: #FF4081 ...
Answered By - donfuxx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.