2016-03-31 22 views
5

YT Advanced Android Espresso cihazında harika bir enstrümantal test eğiticisi buldum. İhtiyaçlarıma küçük ayarlarla oradan kodu aldım.Android enstrümantal testinde Araç çubuğu başlığı nasıl kontrol edilir?

import static android.support.test.InstrumentationRegistry.getInstrumentation; 
import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.assertion.ViewAssertions.matches; 
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 
import static android.support.test.espresso.matcher.ViewMatchers.withChild; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static android.support.test.espresso.matcher.ViewMatchers.withParent; 
import static android.support.test.espresso.matcher.ViewMatchers.withText; 
import static org.hamcrest.core.AllOf.allOf; 

... 

@Test 
public void checkToolbarTitle() { 
    String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops); 
    onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile))); 
} 

Maalesef benim için çalışmaz. Test başarısız oldu:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar) 

Sorun nedir? Bunu başka şekilde nasıl test edebilirim?

cevap

3

ÇÖZÜM

yöntem gayet iyi. Chiu-Ki Chan öğreticisinde de yazdığı gibi, "o belirli bir görüşü" belirleyebilirsin. Bu benim için çalışıyor

import android.widget.Toolbar; 
15

:

import android.support.v7.widget.Toolbar; 

yerine: ANCAK sen emin uygun araç çubuğunu ithal yapmak zorunda

onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))) 
    .check(matches(withText("toolbarTitile"))); 
+2

Bu benim için çalışmadı. Grzegorz Bielanski'nin gönderdiği gibi ithalat oldu. Örneğin, benim için çözümlenemedi. –

+0

Sadece neden aşağı oylama merak ediyorsun ?! Vakaların çoğunda (oy miktarına bakınız) çalışır ve sizin için işe yaramadığı gerçeği bu cevabı yanlış yapmaz. – denys

+0

"Çoğu durumda" için çalıştığını nasıl anlarım? Deneyimlerim, Android'de çalışanların sürümden sürüme değişmesi ve bugün alakalı bir cevabın yarın alakalı olduğundan emin olmaması. –

1

Ben eğer hatırlamıyorum ben Bunu kendim yazdım, ya da bir yerde bulsaydım, ancak araç çubuğu başlıklarını şöyle kontrol ederim:

public static Matcher<View> withToolbarTitle(CharSequence title) { 
    return withToolbarTitle(is(title)); 
} 

public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) { 
    return new BoundedMatcher<View, Toolbar>(Toolbar.class) { 
     @Override 
     public boolean matchesSafely(Toolbar toolbar) { 
      return textMatcher.matches(toolbar.getTitle()); 
     } 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("with toolbar title: "); 
      textMatcher.describeTo(description); 
     } 
    }; 
} 

Bu, tüm durumlarda geçerlidir. Örnek iddia: onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));

İlgili konular