单击视图检查是否使用 Espresso 启动了新的 Activity

Check if a new Activity is started with Espresso by click on view

我有一个按钮,点击这个,开始activity A.

 startActivityForResult(Intent(this, A::class.java)

我需要在单击按钮时检查浓缩咖啡测试,是否启动 activity A?

onView(withId(R.id.button))
       .check(matches(isDisplayed()))
       .check(matches(isEnabled()))
       .perform(click())

        // check is this A Activity start or not?

您可以使用 espresso-intents 包。

首先,尝试将最新版本添加到您的 build.gradle:

   androidTestImplementation "androidx.test.espresso:espresso-intents:3.1.1"

然后,使用IntentsTestRule检查您的意图是否启动:

    @get:Rule
    val intentRule = IntentsTestRule(MainActivity::class.java)

    @Test
    fun verify_FakeActivity_is_started() {
        onView(withId(R.id.button))
            .check(matches(isDisplayed()))
            .check(matches(isEnabled()))
            .perform(click())

        intended(hasComponent(FakeActivity::class.java.name))
    }