2016-08-12 26 views
8

Tôi có một hoạt động mà một số hộp thoại đang mở. Trên hộp thoại này có một spinner. Tôi muốn chọn một giá trị cụ thể trong spinner này, nhưng tôi nhận được ngoại lệ sau đây:RunTimeException trong Android espresso khi chọn spinner trong hộp thoại

java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root: 
Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#3 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1280, height=752, has-focus=true, has-focusable=true, has-window-focus=false, 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}} 
.All Roots: 
Root{[email protected], [email protected], has-window-focus=true, layout-params-type=1002, layout-params-string=WM.LayoutParams{(91,111)(509x113) gr=#10000033 sim=#1 ty=1002 fl=#1860200 fmt=-3 wanim=0x10302e3 surfaceInsets=Rect(0, 0 - 0, 0) (manual)}, decor-view-string=PopupDecorView{id=-1, visibility=VISIBLE, width=509, height=113, 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}} 
Root{[email protected], [email protected], has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#3 ty=2 fl=#1800002 fmt=-3 wanim=0x10303e6 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=616, height=490, has-focus=true, has-focusable=true, has-window-focus=false, 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}} 
Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#3 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1280, height=752, has-focus=true, has-focusable=true, has-window-focus=false, 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}} 
at android.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:99) 
at android.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:69) 
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:23) 
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:9) 
at android.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:68) ..... 

Mã của tôi:

protected void selectSpinnerValue(int id, String value) { 
    onView(withId(id)).perform(click()); 
    onData(allOf(is(instanceOf(String.class)), is(value))).perform(click()); 
} 

Trường hợp ngoại lệ xảy ra trên dòng onData (allOf ....)

Tôi có thể nói với espresso như thế nào trong hộp thoại?

+0

Hãy thử sử dụng isPlatformPopup(), nó sẽ phù hợp với cửa sổ spinner được kết xuất trong – jeprubio

+0

Cảm ơn bạn, làm thế nào để sử dụng này.? onData (isPlatformPopup (allOf (là (instanceOf (String.class)))) không hoạt động – Peter

+1

Tôi đang viết từ điện thoại di động của mình bây giờ, tôi nghĩ rằng onData là OK và espresso đang ném ngoại lệ khi thực thi mã onView và một cái gì đó như: onView (withId (id)). inRoot (isPlattormPopup()) thực hiện (click()); nên làm việc. Hãy thử, tôi đã thấy ngoại lệ này trước đây. – jeprubio

Trả lời

4

Hãy thử điều này:

onData(allOf(is(instanceOf(String.class)), is(value))) 
    .inRoot(isPlatformPopup()).perform(click()); 
6

Tôi đã có những lỗi tương tự khi tôi đã cố gắng để phù hợp với một mục bên trong một spinner bên trong một hộp thoại-fragment. này giúp:

onView(withText(value)).inRoot(isPlatformPopup()).perform(click()); 
+0

Wow, cảm ơn rất nhiều! Đó chính là vấn đề của tôi –

Các vấn đề liên quan