How*_*ieH 15 testing android android-espresso
是否可以使用浓缩咖啡检查背景颜色是否与给定颜色匹配?
我制作了一个自定义匹配器,类似于@Irfan建议的,谢谢!
public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) {
return buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) {
final int[] color = new int[1];
return new BoundedMatcher<Object, Button>( Button.class) {
@Override
public boolean matchesSafely(final Button actualObject) {
color[0] =((ColorDrawable) actualObject.getBackground()).getColor();
if( expectedObject.matches(color[0])) {
return true;
} else {
return false;
}
}
@Override
public void describeTo(final Description description) {
// Should be improved!
description.appendText("Color did not match "+color[0]);
}
};
}
Run Code Online (Sandbox Code Playgroud)
den*_*nys 29
在我的测试中,我有以下用于测试EditText
颜色的匹配器:
public static Matcher<View> withTextColor(final int color) {
Checks.checkNotNull(color);
return new BoundedMatcher<View, EditText>(EditText.class) {
@Override
public boolean matchesSafely(EditText warning) {
return color == warning.getCurrentTextColor();
}
@Override
public void describeTo(Description description) {
description.appendText("with text color: ");
}
};
}
Run Code Online (Sandbox Code Playgroud)
用法是:
onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));
Run Code Online (Sandbox Code Playgroud)
我不确定,但我们可以检索一些元素的颜色,如按钮和文本视图`
Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();
Run Code Online (Sandbox Code Playgroud)
你可以这样试试
ColorDrawable b_color = (ColorDrawable) button.getBackground();
Run Code Online (Sandbox Code Playgroud)
然后
int color = b_color.getColor();
if (color == R.color.green) {
log("color is green");
}
Run Code Online (Sandbox Code Playgroud)
希望这会让你开始.
我迟到了,但这可能对其他人有帮助
public static Matcher<View> matchesBackgroundColor(final int expectedResourceId) {
return new BoundedMatcher<View, View>(View.class) {
int actualColor;
int expectedColor;
String message;
@Override
protected boolean matchesSafely(View item) {
if (item.getBackground() == null) {
message = item.getId() + " does not have a background";
return false;
}
Resources resources = item.getContext().getResources();
expectedColor = ResourcesCompat.getColor(resources, expectedResourceId, null);
try {
actualColor = ((ColorDrawable) item.getBackground()).getColor();
}
catch (Exception e) {
actualColor = ((GradientDrawable) item.getBackground()).getColor().getDefaultColor();
}
finally {
if (actualColor == expectedColor) {
Timber.i("Success...: Expected Color " + String.format("#%06X", (0xFFFFFF & expectedColor))
+ " Actual Color " + String.format("#%06X", (0xFFFFFF & actualColor)));
}
}
return actualColor == expectedColor;
}
@Override
public void describeTo(final Description description) {
if (actualColor != 0) { message = "Background color did not match: Expected "
+ String.format("#%06X", (0xFFFFFF & expectedColor))
+ " was " + String.format("#%06X", (0xFFFFFF & actualColor));
}
description.appendText(message);
}
};
}
Run Code Online (Sandbox Code Playgroud)
我还以十六进制格式记录了预期的和实际的颜色,这可能会有所帮助。
用法: onView(withId(R.id.viewId)).check(matches(matchesBackgroundColor(R.color.colorId)));
归档时间: |
|
查看次数: |
11855 次 |
最近记录: |