尝试学习Android Espresso测试开发。
我有一个列表,其中包含两个相同的项目(相同的资源ID,相同的文本,只有一个列表)。
目前我这样做(可能非常错误):
DataInteraction dataInteraction = onData(anything()).inAdapterView(withText("Test1"));
dataInteraction.atPosition(0).perform((click()));
此结果是例外:
I/TestRunner: ----- begin exception -----
I/TestRunner: android.support.test.espresso.AmbiguousViewMatcherException: 'with text: is "Test1"' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
布局是这样的:
...
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=720, height=128, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=65.0, child-count=2}
|
+---------->AppCompatImageView{id=xxxxxxxxx, res-name=image, visibility=VISIBLE, width=80, height=61, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=33.0}
|
+---------->AppCompatTextView{id=xxxxxxx01, res-name=title, visibility=VISIBLE, width=162, height=46, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=80.0, y=41.0, text=Test1, input-type=0, ime-target=false, has-links=false} ****MATCHES****
|
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=720, height=65, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=193.0, child-count=2}
|
+---------->AppCompatTextView{id=xxxxxxxxx, res-name=text, visibility=VISIBLE, width=720, height=63, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=All Tests, input-type=0, ime-target=false, has-links=false}
|
+---------->View{id=-1, visibility=VISIBLE, width=720, height=2, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0}
|
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=720, height=128, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=258.0, child-count=2}
|
+---------->AppCompatImageView{id=xxxxxxxxx, res-name=image, visibility=VISIBLE, width=80, height=61, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=33.0}
|
+---------->AppCompatTextView{id=xxxxxxx01, res-name=title, visibility=VISIBLE, width=162, height=46, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=80.0, y=40.0, text=Test1, input-type=0, ime-target=false, has-links=false} ****MATCHES****
...
列表是类型:
ExpandableListView
它包含类型:
的类class TestType implements Serializable, Searchable
当我有多个具有相同信息的实例时,如何按文本或资源ID单击列表中的项目?
答案 0 :(得分:0)
最后想通了我可以点击它:
onData(allOf(is(instanceOf(TestType.class)))).atPosition(0).perform(click());
然而,更好的方法是包含TestType中包含的文本或资源ID的匹配。实例