使用Espresso在集成测试中查找页脚

时间:2015-05-08 14:01:54

标签: android android-espresso

我有一个自定义适配器,如MyCustomAdapter,它扩展了BaseAdapter。 我还有一个listView,我添加了一个footerView:

    protected ListView mItemsList;

    // Adapter
    protected myCustomAdapter mAdapter;

   @VisibleForTesting
    public static final String FOOTER = "FOOTER";

View footerView = LayoutInflater.from(this).inflate(R.layout.footer_view, mItemsList, false);
mItemsList.addFooterView(footerView, FOOTER, true);

现在我想找到页脚并用Espresso进行测试。这是我正在使用的代码:

public void testClickFooter() {

    onData(isFooter()).perform(click());
}

private Matcher<Object> isFooter() {
  // This depends on Activity.FOOTER being passed as data in the addFooterView method.
     return allOf(is(instanceOf(String.class)), is(ChecklistBaseActivity.FOOTER));
}

失败,我收到以下错误:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=540, height=960, has-focus=true, has-focusable=true, 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, child-count=1}
|
+->ActionBarOverlayLayout{id=16909238, res-name=action_bar_overlay_layout, visibility=VISIBLE, width=540, height=960, has-focus=true, has-focusable=true, 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, child-count=3}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=540, height=850, has-focus=true, has-focusable=true, 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=110.0, child-count=1}
|
+--->RelativeLayout{id=-1, visibility=VISIBLE, width=540, height=850, has-focus=true, has-focusable=true, 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, child-count=3}
|
+---->RelativeLayout{id=2131361812, res-name=header, visibility=VISIBLE, width=540, height=60, 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, child-count=3}
|
+----->NetworkImageView{id=2131361889, res-name=image_header_thumbnail, visibility=INVISIBLE, width=54, height=48, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=6.0, y=6.0}
|
+----->TextView{id=2131361890, res-name=text_header_title, visibility=VISIBLE, width=408, height=60, 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=66.0, y=0.0, text=Add new checklist, input-type=0, ime-target=false}
|
+----->Space{id=2131362013, res-name=space_header, visibility=INVISIBLE, width=54, height=48, 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=480.0, y=6.0}
|
+---->ListView{id=2131361813, res-name=list_checklist_items, visibility=VISIBLE, width=516, height=718, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=12.0, y=60.0, child-count=2} ****MATCHES****
|
+----->RelativeLayout{id=-1, visibility=VISIBLE, width=516, height=167, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=true, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+------>EditText{id=2131362006, res-name=text_checklist_title, visibility=VISIBLE, width=516, height=57, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=true, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x4001 imeOptions=0xc000006 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x2000 hintText=Title label=null packageName=null fieldId=0 fieldName=null extras=null ], x=0.0, y=12.0, text=, hint=Title, input-type=16385, ime-target=false}
|
+------>LinearLayout{id=2131362007, res-name=checklist_template_extras_container, visibility=VISIBLE, width=516, height=60, 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=true, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=81.0, child-count=2}
|
+------->FrameLayout{id=2131362011, res-name=layout_image_selection, visibility=VISIBLE, width=332, height=60, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=true, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+-------->TextView{id=2131362012, res-name=text_choose_image, visibility=VISIBLE, width=168, height=60, 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=true, root-is-layout-requested=false, has-input-connection=false, x=82.0, y=0.0, text=Välj bild/ikon, input-type=0, ime-target=false}
|
+------->Spinner{id=2131361848, res-name=spinner_color_selection, visibility=INVISIBLE, width=172, height=60, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=true, root-is-layout-requested=false, has-input-connection=false, x=344.0, y=0.0, child-count=0} ****MATCHES****
|
+------>View{id=2131362008, res-name=border_checklist_title, visibility=VISIBLE, width=516, 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=true, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=153.0}
|
+----->LinearLayout{id=2131361978, res-name=layout_add_new_checklist_item, visibility=VISIBLE, width=516, height=125, has-focus=false, has-focusable=true, 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=169.0, child-count=3}
|
+------>View{id=2131361979, res-name=list_footer, visibility=VISIBLE, width=516, height=8, 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}
|
+------>LinearLayout{id=2131361980, res-name=checkbox_checklist_item_new, visibility=VISIBLE, width=516, height=57, has-focus=false, has-focusable=true, 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=8.0, child-count=2}
|
+------->CheckBox{id=2131361981, res-name=checkbox_checklist_item_done, visibility=VISIBLE, width=36, height=36, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=false, 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=11.0, text=, input-type=0, ime-target=false, is-checked=false}
|
+------->EditText{id=2131361982, res-name=text_checklist_item_description, desc=Description, visibility=VISIBLE, width=480, height=57, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x4001 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x2000 hintText=null label=null packageName=null fieldId=0 fieldName=null extras=null ], x=36.0, y=0.0, text=, input-type=16385, ime-target=false}
|
+------>ImageButton{id=2131361983, res-name=button_add_checklist_item, visibility=VISIBLE, width=60, height=60, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, 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=65.0}
|
+---->RelativeLayout{id=2131361814, res-name=layout_button_row, visibility=VISIBLE, width=540, height=72, 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=778.0, child-count=2}
|
+----->View{id=2131361976, res-name=footer_divider, visibility=VISIBLE, width=540, 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=0.0}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=540, height=70, 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=2.0, child-count=2}
|
+------>TextView{id=2131361917, res-name=btn_cancel, visibility=VISIBLE, width=397, height=60, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=9.0, text=Cancel, input-type=0, ime-target=false}
|
+------>TextView{id=2131361977, res-name=btn_add, visibility=VISIBLE, width=143, height=64, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, 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=397.0, y=6.0, text=Add, input-type=0, ime-target=false}
|
+-->ActionBarContainer{id=16909239, res-name=action_bar_container, visibility=VISIBLE, width=540, height=72, has-focus=false, has-focusable=true, 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=38.0, child-count=2}
|
+--->ActionBarView{id=16909240, res-name=action_bar, visibility=VISIBLE, width=540, height=72, has-focus=false, has-focusable=true, 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, child-count=2}
|
+---->LinearLayout{id=-1, desc=Keeptrack, Navigate up, visibility=VISIBLE, width=217, height=72, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, 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=2}
|
+----->HomeView{id=-1, visibility=VISIBLE, width=84, height=72, 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, child-count=2}
|
+------>ImageView{id=16908981, res-name=up, visibility=VISIBLE, width=24, height=24, 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=24.0}
|
+------>ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=48, height=48, 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=30.0, y=12.0}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=133, height=37, 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=84.0, y=17.0, child-count=2}
|
+------>TextView{id=16909003, res-name=action_bar_title, visibility=VISIBLE, width=121, height=37, 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=Keeptrack, input-type=0, ime-target=false}
|
+------>TextView{id=16909004, res-name=action_bar_subtitle, visibility=GONE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false}
|
+---->ActionMenuView{id=-1, visibility=VISIBLE, width=0, height=72, 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=540.0, y=0.0, child-count=0}
|
+--->ActionBarContextView{id=16909241, res-name=action_context_bar, visibility=GONE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
+-->ActionBarContainer{id=16909242, res-name=split_action_bar, visibility=GONE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:69)
at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:40)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:159)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.doPerform(ViewInteraction.java:90)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.perform(ViewInteraction.java:73)
at com.google.android.apps.common.testing.ui.espresso.DataInteraction.load(DataInteraction.java:135)
at com.google.android.apps.common.testing.ui.espresso.DataInteraction.perform(DataInteraction.java:112)
at se.comai.keeptrack.espresso.activity.ChecklistTemplateActivityTest.testClickFooter(ChecklistTemplateActivityTest.java:40)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)

为什么会发生,解决方案是什么?

1 个答案:

答案 0 :(得分:0)

如错误显示问题视图标有'**** MATCHES ****',如果您按照错误在ListView前面找到'**** MATCHES ***',在层次结构中找到Spinner

为了解决您有多个适配器视图的问题,您可以调用ViewMatchers.inAdapterView()

onData(isFooter()).inAdapterView(withId(R.id.list)).perform(click());

你已经完成了。