我在测试斜体样式显示单词时遇到问题。有人可以给我提供任何显示文字样式的示例代码吗?我在 android studio 中使用 Espresso 和 JUnit 4。我非常感谢您的合作。谢谢你
请尝试以下解决方案。它可能对你有用。 核心思想是考虑为您的案例使用自定义 ViewMatcher。
public static Matcher<View> withItalicStyle(final int resourceId) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("has Italic Text with resource" );
}
@Override
public boolean matchesSafely(View view) {
TextView textView = (TextView) view.findViewById(resourceId);
return (textView.getTypeface().getStyle() == Typeface.ITALIC);
}
};
}
在你的测试用例中,你可以
onView(CustomMatchers.withItalicStyle(R.id.yourResourceId)).check(isDisplayed());
基于 Wae 的解决方案,但使用
BoundedMatcher
(和 Kotlin):
fun hasTextStyle(textStyle: Int): Matcher<View> {
return object : BoundedMatcher<View,TextView>(TextView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has specified text style")
}
override fun matchesSafely(item: TextView): Boolean {
return item.typeface.style == textStyle
}
}
}
像
onView(withId(R.id.example)).check(matches(hasTextStyle(Typeface.ITALIC)))
一样使用它