Espresso - 如何在执行某项操作后检查某项活动是否已启动?

时间:2014-09-23 15:12:07

标签: android android-activity android-espresso

以下是我的Espresso测试用例之一。

    public void testLoginAttempt() {
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com"));
        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)中的视图是否可见。如果可见,我将假设活动成功打开。 但它似乎没有像我预期的那样工作。 有没有更好的方法来检查是否成功启动了新活动而不是检查该活动中的视图是否可见? 感谢

8 个答案:

答案 0 :(得分:59)

您可以使用:

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方法调用它。

答案 1 :(得分:12)

尝试:

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

另外,请记住

如果在java.lang.NullPointerException

之前未调用Intents.init(),则会引发{p> intended()

答案 2 :(得分:7)

问题是您的应用程序在您单击登录按钮后执行网络操作。 Espresso默认不处理(等待)网络呼叫。您必须实现自定义IdlingResource,它将阻止Espresso继续进行测试,直到IdlingResource在空闲状态下返回,这意味着网络请求已完成。查看Espresso样本页面 - https://google.github.io/android-testing-support-library/samples/index.html

答案 3 :(得分:7)

尝试

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

查看response from @riwnodennyk

答案 4 :(得分:6)

您可以按照以下方式执行此操作:

    @Test
public void testLoginAttempt() {
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@example.com"));
    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();
}
如果未调用java.lang.NullPointerException,则抛出

Intents.init()

答案 5 :(得分:5)

确保Espresso意图库位于gradle依赖项

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

然后在测试文件中导入这两个

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

答案 6 :(得分:0)

我使用这种方法:

// 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()))
    }

答案 7 :(得分:-5)

@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {

    @Test
    public void clickingLogin_shouldStartLoginActivity() {
        WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
        activity.findViewById(R.id.login).performClick();

        Intent expectedIntent = new Intent(activity, LoginActivity.class);
        assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
    }
}