簡體   English   中英

Android espresso匹配視圖在RecyclerView中重復

[英]Android espresso match view duplicated inside RecyclerView

我有一個具有以下布局的回收者視圖:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_primary"/>

</RelativeLayout>

我有一個問題,因為在測試過程中,我有一會兒在回收者視圖中顯示兩個具有相同文本的視圖,我需要檢查其中一個是否可見。

匹配視圖的代碼如下:

onView(withId(R.id.title)).check(matches(withText("Test")));

濃縮咖啡之所以失敗,是因為它發現了多個符合匹配標准的視圖。

如何匹配所需的特定視圖?

一種可能的解決方案是在每行中設置一個不同的標簽,然后使用此標簽與espresso匹配。

假設正在顯示的數據保留在數據庫中。 您可以將模型ID用作標簽,這樣,您將使回收器視圖中的每一行都有唯一的約束,您可以在匹配器中使用該約束。

該代碼將是這樣的:

onView(allOf(
            withId(R.id.title),
            allOf(
                isDescendantOfA(withTagValue(is((Object)model.getId())))),
                withText(model.getTitle())))
       .check(matches(isDisplayed()))

由於您需要使用匹配器中的文本,因此可以檢查視圖是否可見,以便將子句添加到測試中。

您可以在此處閱讀說明此內容的文章: https : //medium.com/tech-track/validating-views-by-tag-with-espresso-50d3f47b14a7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM