2015-12-03 7 views
15

私はEspressoを使用してActionPageのテキストをテストしようとしています。しかし、Ui Automation Viewerを実行すると、ActionPageはActionViewではなくViewとして表示され、TextViewは表示されません。Android Espresso Ui TestはActionPageのラベルテキストを確認します

私はこのようなActionLabelテキストをチェックしようとしたが、それは動作しません:

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

私はonView(withId(R.id.actionPage))でそれを見つけることができますので、私は私のActionPageのIDを持っているが、私はその子にアクセスする方法を知りませんActionLabelテキストを取得します。私は、カスタム照合を書いてみましたが、これも動作しません:

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

誰かが私を助けてもらえ、ActionLabelは自身とそのないのTextViewでIDを持っていないようです...どのように私は確認することができますそれの中のテキスト?

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

このレイアウトの少なくとも1つにコンテンツ記述またはIDをプログラムで追加することはできますか?キャッチする方がはるかに簡単です – piotrek1543

答えて

6

あなたはallOfwithParentを使用することができます。

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

残念ながらActionLabelgetText()を経由してそのテキストを公開していないので、代わりに標準withText()マッチャーのは、カスタムを記述する必要があります反射を使用するもの:

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

さらに詳しい情報:http://blog.sqisland.com/2015/05/espresso-match-toolbar-title.html

ので、あなたが反射せずにそれをテストすることができますパブリックメソッドActionLabel.getText()を追加するには、Googleを要求するために、バグを報告してください。

+0

ありがとう!問題は 'ActionLabel'が' TextView'に割り当てられず、 'ActionLabel'が公開' getText() 'メソッドを持たないということです。 – AlexIIP

+0

'ActionLabel'はあなたのカスタムビューですか?もしそうなら、 'getText()'メソッドを追加し、それを使ってカスタムマッチャーを書くことができます。サンプルカスタムマッチャーについては、ブログのリンクを参照してください。 – chiuki

+0

それは、アンドロイドクラスです。 http://developer.android.com/reference/android/support/wearable/view/ActionPage.html – AlexIIP

関連する問題