0

我在我的項目中使用Espresso庫。在教程和示例中有如何測試適配器和視圖的示例。但我有自定義適配器和自定義特定視圖。 我的觀點:Android。濃咖啡。如何測試自定義適配器中的自定義視圖?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tvCity" 
    style="@style/Widget.TextView.OnlyOneLine" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/ab_city_spinner_inner_padding" 
    android:text="Test" 
    android:textColor="@color/ab_city_spinner_city_text" 
    android:textSize="@dimen/ab_city_spinner_city_text_size" /> 

<TextView 
    android:id="@+id/tvRegion" 
    style="@style/Widget.TextView.OnlyOneLine" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/ab_city_spinner_inner_padding" 
    android:text="Test" 
    android:textColor="@color/ab_city_spinner_region_text" 
    android:textSize="@dimen/ab_city_spinner_region_text_size" /></LinearLayout> 

我的測試:

@SmallTest 
public void testChangeCity() 
{ 
    onView(withId(R.id.spAB)).perform(ViewActions.click()); 
    onView(allOf(withText("City1"), hasSibling(withText("Test")))).perform(ViewActions.click()); 
} 

這是工作。但我現在不知道數據有什麼看法。唧可以打開微調並點擊項目?

回答

2

我找到答案。 的微調找到並點擊微調

Espresso.onView(ViewMatchers?.withId(R.id.spAB)).perform(ViewActions?.click()); 

採取數據

List<City> listCity = getActivity().getCityList(); 

檢查數據

Preconditions.checkNotNull(listCity, "No have any city"); 

比點擊查看

ViewMatchers?.hasSibling(ViewMatchers?.withText(listCity.get(0).getName())))).perform(ViewActions?.click()); 
+1

這麼多你的代碼的丟失 - 例如,getCi在哪裏tyList是否定義? –

相關問題