14

Android 用の Google Espresso を使用して UI テストを書いていますが、コンテンツが Web サービスから非同期に読み込まれる TextView テキストをアサートする方法に行き詰まっています。私の現在のコードは次のとおりです。

public class MyTest extends BaseTestCase<MyActivity>{
    public void setUp() throws Exception {
        // (1) Tell the activity to load 'element-to-be-loaded' from webservice
        this.setActivityIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("data://data/element-to-be-loaded")));
        getActivity();

        super.setUp();
    }

    public void testClickOnReviews(){
        // (2) Check the element is loaded and its name is displayed
        Espresso
            .onView(ViewMatchers.withId(R.id.element_name))
            .check(ViewAssertions.matches(ViewMatchers.withText("My Name")));

        // (3) Click on the details box
        Espresso
            .onView(ViewMatchers.withId(R.id.details_box))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // (4) Wait for the details screen to open
        Espresso
            .onView(ViewMatchers.withId(R.id.review_box));

        // Go back to element screen
        Espresso.pressBack();
    }
}

(1) で、Web サービスから要素をロードするようアクティビティに通知します。(2) では、ビューがコンテンツをアサートするのを待っています。これは、Web サービスがアプリに応答する前に実行されるため、テストが失敗する部分です。

特定のデータが画面に表示されるのを待つよう Espresso に指示するにはどうすればよいですか? または、そのようなテストを書くために別の方法で考えるべきですか?

4

2 に答える 2

19

Espresso で Web サービスの IdlingResource を登録することで、このケースを処理できます。この記事をご覧ください: https://developer.android.com/training/testing/espresso/idling-resource.html

ほとんどの場合、CountingIdlingResource (単純なカウンターを使用してアイドル状態を追跡します) を使用することをお勧めします。このサンプル テストは、これを行う方法を示しています。

于 2014-01-08T23:22:26.033 に答える