2013-10-30 27 views
6

使用Espresso,我希望能够点击ExpandableListView(名为CustomExpandableView)的特定子项。该列表视图创建一组RelativeLayouts(名为MyContainer)。点击浓缩咖啡中ListView的特定子项

理想情况下,我想单击CustomExpandableView中的特定MyContainer。但是,我只需点击第一个就能生活。

MyContainer对象没有我可以引用的唯一ID,但他们的子女可以,例如, - “text = Sample Text Here 1”

我已经尝试了几个不同的使用onData传递类类型,并试图让孩子在特定位置,但它只是不工作。而且,我想避免获取对象并迭代它直到找到合适的孩子。

下面是参考视图层次的部分(I去除从层次结构的非重要信息):

 
+----->CustomExpandableView{} 
| 
+------>LinearLayout{} 
| 
+------->TextView{} 
| 
+------->FrameLayout{} 
| 
+-------->BreadCrumbView{} 
| 
+--------->ImageButton{} 
| 
+--------->TextView{} 
| 
+------>LinearLayout{} 
| 
+------->MyContainer{} 
| 
+-------->ImageView{res-name=thumb, } 
| 
+-------->ImageView{res-name=divider} 
| 
+-------->TextView{res-name=label, text=Sample Text Here 1, input-type=0, ime-target=false} 
| 
+------->MyContainer{} 
| 
+-------->ImageView{res-name=thumb} 
| 
+-------->ImageView{res-name=divider} 
| 
+-------->TextView{res-name=label text=Sample Text Here 2, input-type=0, ime-target=false} 
| 

回答

0

尝试则instanceof加上hasSibling匹配器例如

onView(allOf(is(instanceOf(MyContainer.class)), hasSibling(withText("Sample Text Here 1")))) 
+0

这是返回错误。 OnView需要匹配器,这是返回匹配器。您没有在OnView中指定任何视图对象。 – Khushboo

+0

@Khushboo,所以切换到onData。 –

0

您可以按照Google直接提供的这里EspressoSamples
您必须使用onData()作为例如

onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("STR"), is("item: 50"))) 
    .perform(click()); 
0

当你试图使用onData,咖啡试图寻找不被一些PARAMS查看,但得到的ListViewAdapter并在此适配器搜索数据(通过调用Adapter#getItem)。

但是在ExpandableListView的情况下,获得什么数据并不明显。 为此目的,Espresso提供onData...().usingAdapterViewProtocol(AdapterViewProtocol)。这种方法的javadoc说:

/** * Use a different AdapterViewProtocol if the Adapter implementation does not * satisfy the AdapterView contract like (@code ExpandableListView) */

这AdapterViewProtocol接口的样子:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/google/android/apps/common/testing/ui/espresso/action/AdapterViewProtocol.java。的AdapterViewProtocol实施

例子:https://stackoverflow.com/a/40282991/1282732

后正确地定义它,onData会发现项目来查看您搜索。之后,如果您需要执行操作或检查孩子。你应该onData#onChildView