web-dev-qa-db-ja.com

エスプレッソ-特定のアクションを実行した後にアクティビティが起動したかどうかを確認するにはどうすればよいですか?

以下は私のエスプレッソテストケースの1つです。

    public void testLoginAttempt() {
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("[email protected]"));
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));

        Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
        // AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.
        // Clicking launches a new activity that shows the text entered above. You don't need to do
        // anything special to handle the activity transitions. Espresso takes care of waiting for the
        // new activity to be resumed and its view hierarchy to be laid out.
        Espresso.onView(ViewMatchers.withId(R.id.action_logout))
                .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));

    }

現在、私がしたことは、新しいアクティビティ(R.id.action_logout)のビューが可視かどうかを確認することでした。表示されている場合、アクティビティが正常に開かれたと想定します。しかし、期待どおりに動作しないようです。新しいアクティビティが表示されているかどうかを確認する代わりに、新しいアクティビティが正常に起動されたかどうかを確認するより良い方法はありますか?ありがとう

55
user2062024

次を使用できます。

_intended(hasComponent(YourExpectedActivity.class.getName()));
_

このgradleエントリが必要です:

_androidTestCompile ("com.Android.support.test.espresso:espresso-intents:$espressoVersion")
_

intended()およびhasComponent()のインポート

_import static Android.support.test.espresso.intent.Intents.intended;
import static Android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
_

shubam Guptaが述べたように、Intents.init()を呼び出す前にintended()を呼び出すことを忘れないでください。最終的には_@Before_メソッドで呼び出すことができます。

63
baskara

試してください:

intended(hasComponent(YourActivity.class.getName()));

また、覚えておいてください

Intents.init()の前にintended()が呼び出されない場合、Java.lang.NullPointerExceptionがスローされます

15
Shubham Gupta

で試す

intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class)));

@ riwnodennykからの応答 を見てください

7
lujop

Espresso インテントライブラリがgradle依存関係にあることを確認してください

androidTestImplementation "com.Android.support.test.espresso:espresso-intents:3.0.1"

次に、これら2つをテストファイルにインポートします

import static Android.support.test.espresso.intent.Intents.intended
import static Android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent

次に、テストクラスに IntentsTestRule を追加します

@Rule
@JvmField
val mainActivityRule = IntentsTestRule(MainActivity::class.Java)

最後に、アクティビティが意図を開始したことを確認します

@Test
fun launchActivityTest() {
    onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
            .perform(click())

    intended(hasComponent(WonderfulActivity::class.Java!!.getName()))
}
7
s-hunter

問題は、ログインボタンをクリックした後、アプリケーションがネットワーク操作を実行することです。 Espressoは、デフォルトで終了するネットワーク呼び出しを処理(待機)しません。 IdlingResourceがIdle状態に戻るまでEspressoがテストを続行するのをブロックするカスタムIdlingResourceを実装する必要があります。つまり、ネットワーク要求が終了します。 Espressoサンプルページをご覧ください- https://google.github.io/Android-testing-support-library/samples/index.html

7
denys

次のように実行できます。

_    @Test
public void testLoginAttempt() {
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("[email protected]"));
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));

    Intents.init();
    Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
    Intents.release();
}
_

Intents.init()が呼び出されない場合、_Java.lang.NullPointerException_がスローされます。

6
Abdul Wadood

私はこのアプローチを使用します:

// The IntentsTestRule class initializes Espresso Intents before each test, terminates the Host activity, and releases Espresso Intents after each test
    @get:Rule
    var tradersActivity: IntentsTestRule<TradersActivity> = IntentsTestRule(TradersActivity::class.Java)
    @get:Rule
    var jsonViewActivity: IntentsTestRule<JsonViewActivity> = IntentsTestRule(JsonViewActivity::class.Java)

    @Test
    fun scrollToItemAndClick() {
     onView(withId(R.id.tradersRecyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(ITEM_POS, click()))

        // check is activity was started
        intended(hasComponent(JsonViewActivity::class.Java.getName()))
    }
0
a_subscriber