2015-12-03 16 views
15

Espresso kullanarak bir ActionPage metnini test etmeye çalışıyorum. Ancak, Ui Automation Viewer'ı çalıştırdığımda, ActionPage'in bir ActionView yerine View olarak gösterildiğini ve TextView içermediğini görebiliyorum.Android Espresso Ui Testin etiket metnini doğrulayın ActionPage

böyle ActionLabel metin için denetimi denedim ama bu işe yaramazsa: Ben bu yüzden onView(withId(R.id.actionPage)) ile bulabilirsiniz benim ActionPage için bir kimliği var ama onun çocukları erişmek için nasıl bilmiyorum

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

ActionLabel metnine ulaşmak için. Özel bir eşleyici yazmaya çalıştım ama bu da işe yaramazsa:

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

birisi, ActionLabel nasıl kontrol edebilirim ... kendisi ve onun değil TextView tarafından bir kimliği olması görünmüyor bana yardım eder misiniz içindeki metin? Maalesef ActionLabelgetText() aracılığıyla metnini göstermiyor

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

, bu yüzden yerine standart withText() Eşleştirici nedeniyle, özel bir yazmak zorunda:

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

Bu düzenlerden en az birine programlı içerik açıklaması veya kimliği eklemek mümkün mü? – piotrek1543

cevap

6

Sen allOf ile withParent kullanabilirsiniz yansıma kullanarak bir tane:

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

diğer bilgiler: http://blog.sqisland.com/2015/05/espresso-match-toolbar-title.html

ActionLabel.getText() böylece yansıma olmadan test edebilirsiniz genel bir yöntem eklemek için Google'a istemek için bir hata başvurusunda bulunun.

+0

'u yakalamak çok daha kolay olurdu Teşekkürler! Sorun 'ActionLabel'' TextView'’e atanamaz ve 'ActionLabel''de public' getText() 'yöntemi yoktur. – AlexIIP

+0

'ActionLabel' özel görünümünüz mü? Eğer öyleyse, 'getText()' yöntemini ekleyebilir ve bunu kullanarak özel bir eşleştirici yazabilirsiniz. Örnek özel bir eşleştirici için blog bağlantısına bakın. – chiuki

+0

Onun değil, bir android sınıfı. http://developer.android.com/reference/android/support/wearable/view/ActionPage.html – AlexIIP