Espresso - How can I check if an activity is launched after performing a certain action?

后端 未结 8 1506
北海茫月
北海茫月 2020-12-02 15:34

the following is one of my Espresso test cases.

    public void testLoginAttempt() {
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(View         


        
相关标签:
8条回答
  • 2020-12-02 16:00

    Make sure the Espresso intent library is in the gradle dependencies

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

    Then import these two in your test file

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

    Then add IntentsTestRule in your test class

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

    Finally check the activity has launched intent

    @Test
    fun launchActivityTest() {
        onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
                .perform(click())
    
        intended(hasComponent(WonderfulActivity::class.java!!.getName()))
    }
    
    0 讨论(0)
  • 2020-12-02 16:04

    You may do it as follows:

        @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 is thrown if Intents.init() is not called.

    0 讨论(0)
提交回复
热议问题