Espresso Android: 单击子线性布局下的图像

Espresso Android: Click on Image under sub Linear Layout

我正在使用 Espresso 进行 Android 测试,想点击线性布局下的 imageView,问题是这个线性布局没有唯一 ID

这是XML:

--LinearLayout (This one have Unique ID)
      --LinearLayout1 (No ID)
             --RelativeLayout (No ID)
                  --TextView
                  --ImageView
             --RelativeLayout (No ID)
                  --TextView
                  --ImageView
      --LinearLayout2 (No ID)
             --RelativeLayout (No ID)
                  --TextView
                  --ImageView
             --RelativeLayout (No ID)
                  --TextView
                  --ImageView
      --LinearLayout3 (No ID)
             --RelativeLayout (No ID)
                  --TextView
                  --ImageView
              -RelativeLayout (No ID)
                  --TextView
                  --ImageView

也就是说,只有顶层的LinearLayout有ID,下面还有3个没有ID的LinearLayout,很难识别。文本和图像视图是动态的。我只想点击第一张或第二张图片。

我依赖 TextView 文本,它在布局中必须是唯一的,并使用 hasSibling():

onView(hasSibling(withText("TextView_unique_text"))).perform(click());

onView(allOf(instanceOf(ImageView.class), hasSibling(withText("TextView_unique_text"))))
    .perform(click());

是的 hasSibling() 对我有用:

onData(anything()).inAdapterView(allOf(withId(R.id.horizontal), hasSibling(withText("Text")))).atPosition(2).onChildView(withId(R.id.image)).perform(click());