5

我有一个显示许多项目的警报对话框。

    private String[] choices; // populated externally

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setItems(choices, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            _callback.onChoiceSelected(which);
            dialog.dismiss();
        }
    });

    _dialog = builder.create();

在不同的情况下(例如,如果您是管理员/访客),您将有不同数量的“选择”。在我的浓缩咖啡测试中,我基本上是在尝试测试存在的项目数量。

public void test_ViewsSetupCorrectly() {

    onView(withId(R.id.settings)).perform(click());
    onView(ViewMatchers.withChild(withClassName(Matchers.equalToIgnoringCase("TextView")))).check(matches(isDisplayed()));

    /*// What I would ideally like ... but doesn't work as well :( :
      onView(withClassName(equalToIgnoringWhiteSpace("DialogTitle"))).check(matches(hasProperty("child-count",
                                                                                                      is(new Integer(1))
                                                                                                      )));*/
}

这些基本上抛出一个异常,如下所示:

我认为 onData 调用仅用于执行操作?我不一定要执行某项操作。谁能帮我实际测试一下?

  com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: has child: with class name: eqIgnoringCase("TextView")
  If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:com.android.internal.app.AlertController$RecycleListView@5389ef7c

  View Hierarchy:
  +>DecorView{id=-1, visibility=VISIBLE, width=684, height=226, 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}
  |
  +->FrameLayout{id=-1, visibility=VISIBLE, width=684, height=226, 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=2}
  |
  +-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=684, height=226, 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}
  |
  +--->LinearLayout{id=16908874, res-name=parentPanel, visibility=VISIBLE, width=652, height=226, 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=16.0, y=0.0, child-count=4}
  |
  +---->LinearLayout{id=16908875, res-name=topPanel, 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=3}
  |
  +----->View{id=16908885, res-name=titleDividerTop, 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}
  |
  +----->LinearLayout{id=16908876, res-name=title_template, 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=2}
  |
  +------>ImageView{id=16908294, res-name=icon, 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}
  |
  +------>DialogTitle{id=16908877, res-name=alertTitle, visibility=VISIBLE, 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}
  |
  +----->View{id=16908878, res-name=titleDivider, 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}
  |
  +---->LinearLayout{id=16908882, res-name=contentPanel, visibility=VISIBLE, width=652, height=226, 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}
  |
  +----->RecycleListView{id=16909101, res-name=select_dialog_listview, visibility=VISIBLE, width=620, height=194, 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=16.0, y=16.0, child-count=2}
  |
  +------>TextView{id=16908308, res-name=text1, visibility=VISIBLE, width=620, height=96, 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=Edit Info, input-type=0, ime-target=false}
  |
  +------>TextView{id=16908308, res-name=text1, visibility=VISIBLE, width=620, height=96, 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=98.0, text=Use gallery photo, input-type=0, ime-target=false}
  |
  +---->FrameLayout{id=16908884, res-name=customPanel, 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=1}
  |
  +----->FrameLayout{id=16908331, res-name=custom, visibility=VISIBLE, 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}
  |
  +---->LinearLayout{id=16908879, res-name=buttonPanel, 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=1}
  |
  +----->LinearLayout{id=-1, visibility=VISIBLE, 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=3}
  |
  +------>Button{id=16908314, res-name=button2, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, 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}
  |
  +------>Button{id=16908315, res-name=button3, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, 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}
  |
  +------>Button{id=16908313, res-name=button1, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, 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}
  |
  +-->ViewStub{id=16909076, 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}
  |
  at dalvik.system.VMStack.getThreadStackTrace(Native Method)
  at java.lang.Thread.getStackTrace(Thread.java:591)
  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.check(ViewInteraction.java:133)
  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:190)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
  at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
4

3 回答 3

9

这就是我想出的添加方法到您使用的任何自定义匹配器库。

public static Matcher<View> hasChildren(final Matcher<Integer> numChildrenMatcher) {
        return new TypeSafeMatcher<View>() {

            /**
             * matching with viewgroup.getChildCount()
             */
            @Override
            public boolean matchesSafely(View view) {
                return view instanceof ViewGroup && numChildrenMatcher.matches(((ViewGroup)view).getChildCount());
            }

            /**
             * gets the description
             */
            @Override
            public void describeTo(Description description) {
                description.appendText(" a view with # children is ");
                numChildrenMatcher.describeTo(description);
            }
        };
    }

用法是:

int numKids = 3;  
onView(withId(R.id.view_pager)).check(matches(allOf(
                    isDisplayed(),
                    hasChildren(is(numKids))
            )));
于 2015-12-14T12:05:10.543 回答
0

过去我也遇到过类似的问题,但我将自己的 EditText 控件添加到警报对话框中。这样做时,您可以为要添加到对话框的视图对象分配一个 ID。

AlertDialog.Builder myDialog = new AlertDialog.Builder(this);

myDialog.setTitle(R.string.add_comment);

// Set an EditText view to get user input
final EditText input = new EditText(this);
input.setId(R.id.myEditText);
myDialog.setView(input);

add_comment_text id 在 res/values 的 ids.xml 文件中指定,然后您可以使用它来选择它。

onView(withId(R.id.myEditText)
于 2014-10-07T13:09:59.307 回答
-1

我知道这是一篇很老的帖子,但看起来你想要的就像这里的这篇博文所描述的一样。然后,您可以使用与对话框视图匹配的匹配器访问对话框,并调用与getChildCount()对话框相关的诸如或类似的方法?希望这会对某人有所帮助!

于 2015-09-24T18:28:32.340 回答