2016-08-19 8 views
0

私は、ListViewにプログラムで追加したFooterView内のTextViewを見つけることができないエスプレッソテストを作成しています。エスプレッソテストでそのTextViewを見つける唯一の方法は、このTextViewが存在するかどうかをチェックする前に1秒間。ListViewのListerViewがEspressoを使用して見つかりません

// 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()メソッドでそれを取得しようが、いけない場合、私は、同じ場所でのテスト内のそれを見つけることができますそれはエスプレッソで。

だから私の質問は次のとおりです。 は、私は本当にFooterView内部のTextViewをチェックしたい場合は、テストに合格するのThread.sleep(1000)を呼び出す必要があります。私にとってエスプレッソの大きな利点は、ビューが用意されるまで待つ必要はないということです。自動的にそのエスプレッソ機能は存在しません。

答えて

0

ListViewのFooterViewは、onDataメソッドで見つけることができます。

ここはofficial documentsです。例えば

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()); 
    ... 
} 
+0

私は決してfiではない問題ではありません私のフッタのビュー内のテキストビューを調べるには、少なくとも1秒間トレッドを寝かせた後でしか見つけることができないという問題があります。私の検索にis(LongListActivity.FOOTER)の基準を追加すると、同じ問題が残っています(実際にはidを検索するだけで十分ではありません)。 この問題は、shelfActivity.getActivity()で同じテキストビューを検索しても発生しません。findViewById();その場合、私はすぐに1秒間寝る必要なく、期待されるテキストビューを見つける。 – richard

+0

私はこの問題を理解できません。問題の「FooterView」を見つけられず、上記のコメントの中で「フッタビュー内でテキストビューを見つけられません。 'R.id.listView_footer'はTextViewのIDですか? – nshmura

+0

申し訳ありませんが、説明があまり明確ではなく、今編集しました。私はチェックの前にsleep(1000)を呼び出さないと、フッタービュー内でtextviewを見つけることができません。 – richard

関連する問題