【问题标题】:How to check text typeface style in Android Espresso如何在 Android Espresso 中检查文本字体样式
【发布时间】:2017-10-09 16:39:00
【问题描述】:

在我们的应用程序中,我们有一个登录按钮,其中包含文本“LOGIN”。

当我运行以下测试时,它失败了:

失败是因为“LOGIN”与“Login”不匹配

mText 是“Login”,即使它在按钮本身上显示为“LOGIN”。我认为这是使所有内容都大写的字体样式?如果是这样,有人可以告诉我如何验证正在使用正确的字体样式吗? 或者..我怎样才能拉出实际显示在屏幕上的文本进行比较?

【问题讨论】:

  • “我认为这是字体样式使它全部大写?” - 是的。默认情况下,Material Design 要求按钮标题全部大写,因此它们将其应用于主题级别,适用于基于 Theme.Material- 和 Theme.AppCompat 的主题。

标签: android android-espresso android-uiautomator


【解决方案1】:

我使用以下匹配器来检查文本是否大写:

public static Matcher<View> withTransformedText(final String text) {
    return new BoundedMatcher<View, TextView>(TextView.class) {
        @Override
        public boolean matchesSafely(TextView textView) {
            final TransformationMethod transformationMethod = textView.getTransformationMethod();
            final String transformedText = transformationMethod.getTransformation(textView.getText(), textView).toString();
            return text.equals(transformedText);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with transformed text: " + text);
        }
    };
}

【讨论】:

    【解决方案2】:

    现在,如果我们根据新版本使用默认主题,那么它会将按钮文本显示为大写,但您可以使用 textAllCaps 选项来启用或禁用它。请参考给定的快照

    enter image description here

    【讨论】:

    • 问题是如何在 espresso 中创建测试/匹配器以验证文本是否大写(具有正确的字体样式)
    猜你喜欢
    • 1970-01-01
    • 2013-02-13
    • 2015-10-03
    • 2011-11-22
    • 2015-04-08
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多