2015-12-03 16 views
15

Tôi đang cố gắng kiểm tra văn bản của ActionPage bằng Espresso. Tuy nhiên, khi tôi chạy Trình xem tự động Ui, tôi có thể thấy rằng ActionPage đang được hiển thị dưới dạng dạng xem thay vì một ActionView và nó không có TextView.Thử nghiệm Ui Android Espresso xác minh văn bản nhãn của ActionPage

Tôi đã thử kiểm tra các văn bản ActionLabel như thế này nhưng điều đó không làm việc:

onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText"))); 

Tôi có một id cho ActionPage của tôi để tôi có thể tìm thấy nó với onView(withId(R.id.actionPage)) nhưng tôi không biết làm thế nào để truy cập vào con của nó để lấy văn bản ActionLabel. Tôi đã cố gắng viết một khớp tùy chỉnh nhưng điều này cũng không làm việc:

onView(withId(R.id.actionPage)).check(matches(withChildText("MyText"))); 

static Matcher<View> withChildText(final String string) { 
     return new BoundedMatcher<View, View>(View.class) { 
      @Override 
      public boolean matchesSafely(View view) { 
       ViewGroup viewGroup = ((ViewGroup) view); 
       //return (((TextView) actionLabel).getText()).equals(string); 
       for(int i = 0; i < view.getChildCount(); i++){ 
        View child = view.getChildAt(i); 
        if (child instanceof TextView) { 
         return ((TextView) child).getText().toString().equals(string); 
        } 
       } 
       return false; 
      } 

      @Override 
      public void describeTo(Description description) { 
       description.appendText("with child text: " + string); 
      } 
     }; 
    } 

Có thể ai đó hãy giúp tôi ra, ActionLabel dường như không có một id của chính nó và nó không phải là một TextView ... làm thế nào tôi có thể kiểm tra văn bản bên trong của nó?

+------>FrameLayout{id=-1, visibility=VISIBLE, width=320, height=320, 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=1} 
| 
+------->ActionPage{id=2131689620, res-name=actionPage, visibility=VISIBLE, width=320, height=320, 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} 
| 
+-------->ActionLabel{id=-1, visibility=VISIBLE, width=285, height=111, 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=17.0, y=209.0} 
| 
+-------->CircularButton{id=-1, visibility=VISIBLE, width=144, height=144, 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=88.0, y=65.0} 

enter image description here

+0

Có thể thêm mô tả hoặc id nội dung theo chương trình vào ít nhất một trong các bố cục này không? Sẽ dễ dàng hơn nếu bắt được – piotrek1543

Trả lời

6

Bạn có thể sử dụng withParent với allOf:

onView(
    allOf(
     withParent(withId(R.id.actionPage)), 
     isAssignableFrom(ActionLabel.class))) 
    .check(matches(withActionLabel(is("MyText")))); 

Thật không may ActionLabel không tiếp xúc với văn bản của nó thông qua getText(), vì vậy thay vì tiêu chuẩn withText() khớp, bạn phải viết một tùy chỉnh một phản ánh sử dụng:

private static Matcher<Object> withActionLabel(
    final Matcher<CharSequence> textMatcher) { 
    return new BoundedMatcher<Object, ActionLabel>(ActionLabel.class) { 
    @Override public boolean matchesSafely(ActionLabel label) { 
     try { 
     java.lang.reflect.Field f = label.getClass().getDeclaredField("mText"); 
     f.setAccessible(true); 
     CharSequence text = (CharSequence) f.get(label); 
     return textMatcher.matches(text); 
     } catch (NoSuchFieldException e) { 
     return false; 
     } catch (IllegalAccessException e) { 
     return false; 
     } 
    } 
    @Override public void describeTo(Description description) { 
     description.appendText("with action label: "); 
     textMatcher.describeTo(description); 
    } 
    }; 
} 

Thông tin thêm: http://blog.sqisland.com/2015/05/espresso-match-toolbar-title.html

Hãy gửi một lỗi để yêu cầu Google để thêm một phương pháp nào ActionLabel.getText() để bạn có thể kiểm tra nó mà không suy nghĩ.

+0

Cảm ơn! Vấn đề là 'ActionLabel' không thể gán cho' TextView' và 'ActionLabel' không có phương thức' getText() 'công khai. – AlexIIP

+0

Có phải 'ActionLabel' là chế độ xem tùy chỉnh của bạn không? Nếu vậy, bạn có thể thêm phương thức 'getText()' và viết một trình phù hợp tùy chỉnh bằng cách sử dụng nó. Xem liên kết blog cho một trình phù hợp tùy chỉnh mẫu. – chiuki

+0

Không, nó là một lớp android. http://developer.android.com/reference/android/support/wearable/view/ActionPage.html – AlexIIP

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