Not able to launch the app using Espresso

Viewed 771

I have written a basic android instrumentation code to launch the app using the app activity. The problem is, it launches the app and closes it immediately.

package com.esse.mail;

import android.content.Intent;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class HomeScreenTest 
{
    @Rule
    public ActivityTestRule<HomeScreenActivity> mActivityRule = new 
    ActivityTestRule<>(HomeScreenActivity.class);

    @Before
    public void setUp()
    {
        mActivityRule.launchActivity(new Intent());
    }

    @Test
    public void testCompose()
    {

    }

    @After
    public void tearDown()
    {

    }
}

07/03 13:17:28: Launching HomeScreenTest
No apk changes detected since last installation, skipping installation of /Users/webyog/gem-test/falcon/mail/app/build/outputs/apk/debug/app-debug.apk
$ adb push /Users/webyog/gem-test/falcon/mail/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/com.esse.mail.test
$ adb shell pm install --full -t -r "/data/local/tmp/com.esse.mail.test"
Success
APK installed in 385 ms
Running tests

$ adb shell am instrument -w -r   -e debug false -e class 'com.esse.mail.HomeScreenTest' com.esse.mail.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

I am trying to run this sample activity using android instrumented tests and it is failing to launch the app.

1 Answers

Your code is absolutely right and it is written only to launch the app, But if you want to hold it after launch then you will have to add some line of code in public void testCompose(){} method. You can add lines using onView(withId(R.id.X)); Etc, then it will not suddenly close the app. or you can add this line as well, Thread.sleep(1000);

Related