首页 > 解决方案 > Espresso 检查 TextView 文本和 ImageView Drawable 是否在一起

问题描述

我有一个 ConstraintLayout,它是 GridView 内的一个列表项,因此屏幕上会有多个。每个项目都有各种状态,由drawables表示,每个列表项在drawable上方显示的TextView中都有一个唯一的名称。

基本上我在约束布局中有这两个:

<ImageView
    android:id="@+id/client_state_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/client_state_background"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
        android:id="@+id/device_client"
        style="@style/ClientName"
        android:layout_width="64dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="72dp"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Whereclient_state_background可以是多个可绘制对象之一,以编程方式确定。

我正在尝试运行浓缩咖啡测试,以确保正确的列表项显示状态的可绘制对象。我试过这样做:

 ViewInteraction imageView = onView(allOf(withId(R.id.client_state_image), withParent(withParent(withId(R.id.grid_view))), withDrawable(statusToCheck), isDisplayed()));
        imageView.check(matches(isDisplayed()));

使用从这里绘制的 withDrawable 方法

但是我找不到在我的断言中包含 TextView 的方法。

有没有办法断言 TextView 和 ImageView 一起显示正确的信息?

标签: androidtextviewimageviewandroid-espresso

解决方案


推荐阅读