简体   繁体   English

使用Espresso找不到ListView中的FooterView

[英]FooterView in ListView not found using Espresso

I am writing an espresso test which checks which fails to find a TextView within a FooterView that i added programmatically to a ListView.The only way to find that TextView in the espresso test, is to wait for 1 second before checking if this TextView exists. 我正在编写一个浓缩咖啡测试,该测试检查无法在以编程方式添加到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()));

This TextView inside the FooterView is actually existing, and i can find it inside the test on the same place, if i try to get it with the normal findById() method, but dont find it when i check it with espresso. FooterView中的这个TextView实际上是存在的,如果我尝试使用常规的findById()方法来获取它,则可以在同一位置的测试中找到它,但是当我用espresso检查时找不到它。

So my question is: Do i really have to call Thread.sleep(1000) to pass the test if i want to check for a TextView inside a FooterView. 所以我的问题是:如果我想在FooterView中检查TextView,是否真的必须调用Thread.sleep(1000)通过测试。 For me the great advantage of espresso is, that i do not have to deal with waiting until a view is ready, so is there no espresso feature existing that is doing that automatically? 对我来说,espresso的最大优点是,我不必等待视图就绪就可以解决问题,那么是否不存在自动执行此操作的espresso功能?

You can find FooterView of ListView by onData method. 您可以通过onData方法找到ListView的FooterView。

Here is official documents . 这是正式文件

For example: 例如:

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

Then, you can write a matcher that matches this object: 然后,您可以编写一个与此对象匹配的匹配器:

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

And loading the view in a test is trivial: 在测试中加载视图很简单:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM