检查 TextView 的文本是否与 Android Espresso 完全显示

Mak*_*iev 5 android textview android-layout android-espresso

在 Espresso 中,可以检查视图是否显示:

onView(withText("To create a test configuration in Android Studio, complete the following steps")).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

但是我需要检查的是我的文本TextView是否完全显示(不是它TextView本身)。这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="256dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="#fca"
        android:textSize="48sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如果文本TextView为“To create a test configuration in Android Studio,请完成以下步骤”,则未完全显示:

在此处输入图片说明

如果文本较短,我会看到所有内容:

在此处输入图片说明

TextViewa 放入ScrollView不是一种选择。 自动调整 TextViews也不是一个选项。

当然,我可以使用 Android Studio 中 XML 布局编辑器的预览视图更改文本并确保它完全显示。但是如果我有 6 个语言环境、9 个模拟器和 5 个屏幕要检查呢?6 * 9 * 5 = 270 个屏幕!手动测试这个东西很耗时。

Gau*_*ier 1

虽然我没有直接回答你的问题,但这里有一些建议:

  • 使用伪本地化仅手动测试一次

伪本地化是 Android 中可用的一种测试方法,它将用通常会导致 UI/翻译问题的测试文本替换生产文本。基本上,如果您的应用程序仍然可以使用伪语言环境进行读取,那么您就可以开始了。

有关如何使用它的文档:https://developer.android.com/guide/topics/resources/pseudolocales.html

  • 从支持库开始版本 26.0,您可以在 TextView 上使用自动调整大小以确保始终显示您的测试

TextView 现在能够自动缩小文本字体大小,使整个文本适合其边界。

文档:https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html