我是使用濃咖啡的新手。我真的不能得到我想要使用此代碼來測試:如何檢查視圖是否在特定的RecyclerView項目上可見?
onData(withId(R.id.relativelayout_tag))
.inAdapterView(withId(R.id.recyclerview_tag_list))
.onChildView(withId(R.id.imageview_tag))
.atPosition(1)
.check(matches(isDisplayed()));
凡R.id.imageview_tag
是R.id.relativelayout_tag
一個孩子。 R.id.relativelayout_tag
包含我的適配器項目的全部內容。 R.id.recyclerview_tag_list
是我的RecyclerView
的名稱,我在其中指定了一個特定RecyclerView
Adapter
。
這是一個非常非常基本的測試。下面是用戶程序:
- 選擇,具體,在
RecyclerView
的第一項(我真的不關心什麼 文本視圖)。也不要建議使用視圖文本來識別第一項。 我不關心適配器項目的內容甚至在某些視圖上放置了獨特的標籤。 - 選擇後,應顯示指示器視圖(顯示選擇項目 的ImageView)。
非常基本和簡單。使用Espresso爲這個基本的用戶故事編寫測試非常困難。當我運行特定的測試總是失敗說明這一點:
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "RecyclerView{id=2131624115, res-name=recyclerview_tag_list, visibility=VISIBLE, width=480, height=1032, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=15}"
這是沒有意義的,因爲我有列表中已經顯現。我甚至可以運行這個測試只是罰款:
onView(withId(R.id.recyclerview_tag_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(1, click()));
以下是完整的測試:
@Test
public void shouldTagToggleSelected()
{
onView(withId(R.id.recyclerview_tag_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(1, click()));
onData(withId(R.id.relativelayout_tag))
.inAdapterView(withId(R.id.recyclerview_tag_list))
.onChildView(withId(R.id.imageview_tag))
.atPosition(1)
.check(matches(isDisplayed()));
//onView(withId(R.id.imageview_tag))
// .check(matches(isDisplayed()));
}
我想測試如果指標視圖具有可見性設置爲visible
只在特定的項目什麼(或我選擇的任何項目)。
有什麼想法?也許我錯過了一些深刻的東西。
非常感謝!
嗨我編輯你的答案。它應該是'onView(withId(R.id.recyclerview_tag_list))',我知道這就是你的意思。謝謝我現在能夠通過測試。我仍然有點努力想要得到BoundedMatchers的感覺。如果可能,我儘量避免。這似乎不是這種情況。 –