Start espresso from a different activity

Issue

I have an app that launches splash activity screen when recording a test for espresso. However, I only want to start the espresso from another activity that starts after signing in and everything (NavDrawerActivity). Basically I want to skip signing in and all that. Here is the beginning of my code. Does anyone know how to start the testing from NavDrawerActivity?

@Rule
public ActivityTestRule<SplashScreenActivity> mActivityTestRule 
        new ActivityTestRule<>(SplashScreenActivity.class);

@Test
public void avatarActivityEspressoTest() {
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction appCompatButton  onView(
            allOf(withId(R.id.btn_skip), withText("SKIP"),
                    childAtPosition(
                            childAtPosition(
                                    withId(android.R.id.content),
                                    0),
                            4),
                    isDisplayed()));
    appCompatButton.perform(click());

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
..
..

Solution

From Docs

ActivityTestRule to reduce the amount of boilerplate code you need to write. By using ActivityTestRule, the testing framework launches the activity under test before each test method annotated with @Test and before any method annotated with @Before

Just change the rule as

@Rule
public ActivityTestRule< NavDrawerActivity> mActivityTestRule 
        new ActivityTestRule<>(NavDrawerActivity.class);

Answered By – Pavneet_Singh

Leave a Comment