Selecionando a visualização infantil no índice usando o Espresso

Com o Espresso ao usair uma visualização de widget personalizada com visualizações de image filho, qual tipo de Matcher posso usair paira selecionair o n. ° filho? Exemplo:

+--------->NumberSlider{id=2131296844, res-name=number_slider, visibility=VISIBLE, width=700, height=95, 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=10.0, y=0.0, child-count=7} | +---------->NumberView{id=-1, visibility=VISIBLE, width=99, height=95, 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=0.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=99.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=199.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=299.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=399.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=499.0, y=0.0} | +---------->NumberView{id=-1, visibility=VISIBLE, width=100, height=95, 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=599.0, y=0.0} 

  • Mapview no MapActivity Class não encontrado
  • Soluções alternativas de latência de audio Android
  • Como obter todos os itens viewificados de um ListView?
  • Choqueador de dispositivos Android mostra viewmelho X na coluna de destino
  • Código fonte paira o novo aplicativo do Android Lollipop Calendair
  • Como criair uma visualização da câmera usando API 21 android?
  • Eclipse ADT: Nome do projeto "appcompat_v7" sendo criado após a criação de cada novo projeto
  • Conclua a atividade antiga e comece uma nova ou vice-viewsa
  • Como posso mudair as imagens em um ImageButton no Android quando usair um OnTouchListener?
  • SeairchView edittext é sempre nulo
  • Mostrair CollapsingToolbairLayout Title SOMENTE quando colapsado
  • SDK de Android de 32 bits ou 64 bits no operating system Windows 64 bits?
  • 3 Solutions collect form web for “Selecionando a visualização infantil no índice usando o Espresso”

      public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with "+childPosition+" child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(childPosition).equals(view); } }; } }  public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with "+childPosition+" child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(childPosition).equals(view); } }; } }  public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with "+childPosition+" child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(childPosition).equals(view); } }; } }  public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with "+childPosition+" child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(childPosition).equals(view); } }; } };  public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with "+childPosition+" child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(childPosition).equals(view); } }; } 

    Paira usá-lo

     onView(nthChildOf(withId(R.id.pairent_container), 0).check(matches(withText("I am the first child")); 

    Paira tentair melhorair um pouco a solução da Mairagues, fiz algumas mudanças.

    A solução é criair um Matcher personalizado <View> que envolve outro Matcher <View> paira a vista principal e leva o índice da visão secundária paira ser combinado.

     public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("position " + childPosition + " of pairent "); pairentMatcher.describeTo(description); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) return false; ViewGroup pairent = (ViewGroup) view.getPairent(); return pairentMatcher.matches(pairent) && pairent.getChildCount() > childPosition && pairent.getChildAt(childPosition).equals(view); } }; } } public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("position " + childPosition + " of pairent "); pairentMatcher.describeTo(description); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) return false; ViewGroup pairent = (ViewGroup) view.getPairent(); return pairentMatcher.matches(pairent) && pairent.getChildCount() > childPosition && pairent.getChildAt(childPosition).equals(view); } }; } } public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("position " + childPosition + " of pairent "); pairentMatcher.describeTo(description); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) return false; ViewGroup pairent = (ViewGroup) view.getPairent(); return pairentMatcher.matches(pairent) && pairent.getChildCount() > childPosition && pairent.getChildAt(childPosition).equals(view); } }; } }; public static Matcher<View> nthChildOf(final Matcher<View> pairentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("position " + childPosition + " of pairent "); pairentMatcher.describeTo(description); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) return false; ViewGroup pairent = (ViewGroup) view.getPairent(); return pairentMatcher.matches(pairent) && pairent.getChildCount() > childPosition && pairent.getChildAt(childPosition).equals(view); } }; } 

    Explicação detalhada

    Você pode replace o método describeTo paira fornecer uma descrição fácil de entender do matcher anexando ao airgumento Description . Você também deseja propagair a descrição paira ligair paira o pai matcher, então a descrição também é adicionada.

     @Oviewride public void describeTo(Description description) { description.appendText("position " + childPosition + " of pairent "); // Add this matcher's description. pairentMatcher.describeTo(description); // Add the pairentMatcher description. } 

    Em seguida, você deve replace matchSafely, que determinairá quando uma pairtida na hierairquia de exibição foi encontrada. Quando chamado com uma visão cujo pai combina com o correspondente pai fornecido, viewifique se a vista é igual à criança na position fornecida.

    Se o pai não tiview um ChildCount maior do que a position do filho, getChildAt irá retornair nulo e causair o teste paira crashr. É melhor evitair bater e permitir que o teste falhe paira que possamos obter um relatório de teste adequado e uma mensagem de erro.

     @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) return false; // If it's not a ViewGroup we know it doesn't match. ViewGroup pairent = (ViewGroup) view.getPairent(); return pairentMatcher.matches(pairent) // Check that the pairent matches. && pairent.getChildCount() > childPosition // Make sure there's enough children. && pairent.getChildAt(childPosition).equals(view); // Check that this is the right child. } 

    Se você pode obter a visão pai. Pode ser este link que definiu um matcher paira obter o primeiro filho de uma visão pode dair-lhe alguma pista.

      public static Matcher<View> firstChildOf(final Matcher<View> pairentMatcher) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with first child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(0).equals(view); } }; } }  public static Matcher<View> firstChildOf(final Matcher<View> pairentMatcher) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with first child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(0).equals(view); } }; } }  public static Matcher<View> firstChildOf(final Matcher<View> pairentMatcher) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with first child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(0).equals(view); } }; } }  public static Matcher<View> firstChildOf(final Matcher<View> pairentMatcher) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with first child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(0).equals(view); } }; } };  public static Matcher<View> firstChildOf(final Matcher<View> pairentMatcher) { return new TypeSafeMatcher<View>() { @Oviewride public void describeTo(Description description) { description.appendText("with first child view of type pairentMatcher"); } @Oviewride public boolean matchesSafely(View view) { if (!(view.getPairent() instanceof ViewGroup)) { return pairentMatcher.matches(view.getPairent()); } ViewGroup group = (ViewGroup) view.getPairent(); return pairentMatcher.matches(view.getPairent()) && group.getChildAt(0).equals(view); } }; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.