[英]FooterView in ListView not found using Espresso
我正在編寫一個濃縮咖啡測試,該測試檢查無法在以編程方式添加到ListView的FooterView中找到TextView。在濃縮咖啡測試中找到TextView的唯一方法是等待1秒,然后檢查此TextView是否存在。
// checks if the list view contains an issue that has the text "Do" somewhere
onView(withId(R.id.listView)).check(matches(hasDescendant(withText(containsString("Do")))));
// swipes down to access the load older issues button
onView(allOf(withId(R.id.mainLayout))).perform(swipeUp());
// View listView = shelfActivity.getActivity().findViewById(R.id.listView_footer_textview); // returns the view, even when espresso tells that it does not exist
// Thread.sleep(1000); // need to wait in order to find the footerview of the list view
// check if the load older issues button is here
onView(allOf(withId(R.id.listView_footer_textview), isDisplayed())).check(matches(isDisplayed()));
FooterView中的這個TextView實際上是存在的,如果我嘗試使用常規的findById()方法來獲取它,則可以在同一位置的測試中找到它,但是當我用espresso檢查時找不到它。
所以我的問題是:如果我想在FooterView中檢查TextView,是否真的必須調用Thread.sleep(1000)通過測試。 對我來說,espresso的最大優點是,我不必等待視圖就緒就可以解決問題,那么是否不存在自動執行此操作的espresso功能?
您可以通過onData
方法找到ListView
的FooterView。
這是正式文件 。
例如:
public static final String FOOTER = "FOOTER"; ... View footerView = layoutInflater.inflate(R.layout.list_item, listView, false); ((TextView) footerView.findViewById(R.id.item_content)).setText("count:"); ((TextView) footerView.findViewById(R.id.item_size)).setText(String.valueOf(data.size())); listView.addFooterView(footerView, FOOTER, true);
然后,您可以編寫一個與此對象匹配的匹配器:
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @SuppressWarnings("unchecked") public static Matcher<Object> isFooter() { return allOf(is(instanceOf(String.class)), is(LongListActivity.FOOTER)); }
在測試中加載視圖很簡單:
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData; import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click; import static com.google.android.apps.common.testing.ui.espresso.sample.LongListMatchers.isFooter; public void testClickFooter() { onData(isFooter()) .perform(click()); ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.