2014-05-22 12 views
5

Tôi có hộp thoại cảnh báo hiển thị một số mục.Sử dụng Espresso làm cách nào để kiểm tra số lượng mục trong Hộp thoại cảnh báo của tôi

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(); 

Trong các trường hợp khác nhau (ví dụ: nếu bạn là quản trị viên/khách), bạn sẽ có một số "lựa chọn" khác. Trong bài kiểm tra espresso của tôi, về cơ bản tôi đang cố gắng kiểm tra số lượng mặt hàng hiện tại.

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)) 
                            )));*/ 
} 

Những cơ bản ném một ngoại lệ như vậy:

Tôi đoán cuộc gọi onData duy nhất là để thực hiện những hành động? Tôi không thực hiện hành động nào nhất thiết. Bất cứ ai có thể giúp tôi thực sự kiểm tra điều này?

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:[email protected]c 

    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) 
+0

OnData nên cho phép kiểm tra là tốt. Bạn đã thử một cái gì đó như: onData (allOf (là (instanceOf (String.class)), equalTo (string))) atPosition (lastExpectedIndex) .check (); – Maxwell

Trả lời

0

Tôi đã gặp sự cố tương tự trong quá khứ nhưng tôi đã thêm điều khiển EditText của riêng mình vào hộp thoại cảnh báo. Khi thực hiện theo cách này, bạn có thể gán một ID cho đối tượng xem đang được thêm vào hộp thoại.

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); 

Id add_comment_text được chỉ định trong tệp ids.xml trong res/values ​​và sau đó bạn có thể chọn điều này bằng cách sử dụng.

onView(withId(R.id.myEditText) 
0

Tôi biết điều này là khá một bài cũ, nhưng có vẻ như những gì bạn muốn là một cái gì đó giống như những gì được mô tả bởi blog post here này. Sau đó, bạn có thể truy cập hộp thoại bằng một trình phù hợp khớp với giao diện hộp thoại và gọi một số thứ như getChildCount() hoặc phương thức tương tự như vậy liên quan đến hộp thoại? Hy vọng rằng điều này sẽ giúp một ai đó!

+0

Liên kết đó dường như đã chết. – ThomasW

7

đây là những gì tôi đã đưa ra phương pháp thêm vào bất kỳ thư viện đối sánh tùy chỉnh nào bạn sử dụng.

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); 
      } 
     }; 
    } 

Cách sử dụng:

int numKids = 3; 
onView(withId(R.id.view_pager)).check(matches(allOf(
        isDisplayed(), 
        hasChildren(is(numKids)) 
      ))); 
Các vấn đề liên quan