Android Espresso: How to create test-suite which may launch separate test classes with different activities?

Tags: , , , ,



I have just started studying Espresso tests, but I have searched throw StackOverFlow topics and couldn’t found solution for my needs.


Prehistory of my question:

I have android application with lots of activities. Generally separate UI tests were created for different flows of the app (each flow require separate activity, so that’s why I have implemented tests in a different classes). In my case background and UI usync occurs, that’s why I can’t start from first activity and keep going to the next and next. The only solution which I have found and it really work with current app – is using of idling resources for app activities.

Question: SO now am trying to understand how can I put all these separate classes (each class use own rules and activities for these rules) to the functional suite??


My experience:

1) Positive – using idling resource which require changes in developer’s source code, but there is still only one activity in suite:

@RunWith(AndroidJUnit4.class
public class forSeveralActs {
@Rule
public final ActivityTestRule<TargetedActivity> mTargetedActivityRule = new ActivityTestRule<>(TargetedActivity.class, true, true);`

final String username = "test@mail.com";
final String password = "12345678";
private IdlingResource mIdlingRecourse;

@Before
public void setIdleResource() {
    mIdlingRecourse = mTargetedActivityRule.getActivity().getIdlingResource();
    Espresso.registerIdlingResources(mIdlingRecourse);
}

@Test
public void PassPermission_and_goto_MainActivity() {
    // First activity - LoginActivity
    Espresso.onView(withId(R.id.activity_login_et_login))
            .perform(ViewActions.typeText(username));

    Espresso.onView(withId(R.id.activity_login_et_password))
            .perform(ViewActions.typeText(password))
            .perform(closeSoftKeyboard());

    Espresso.onView(withId(R.id.activity_login_btn_login))
            .check(matches(isDisplayed()))
            .check(matches(isClickable()))
            .perform(ViewActions.click());

  // Here is elements of second activity - MainActivity, without idling methods I can't reach this element
    Espresso.onView(withId(R.id.helpMeCard_title))
            .check(matches(withText("help_me")));
}

@After
public void unregisteredIdlingResource() {
    if (mIdlingRecourse != null) {
        Espresso.unregisterIdlingResources(mIdlingRecourse);
    }
}

So in this example I have several tests and the last one will be performed on the next activity.


2) Negative – I have tried to launch all my tests (classes) though JUnit test-suite, so I just put all my classes (which also includes RULES for tests and ACTIVITIES) to the Junit suite (this scenario doesn’t work at all):

@RunWith(Suite.class)
@Suite.SuiteClasses(
    {
            ESP_test1.class,
            ESP_test2.class,
            ESP_test3.class
    })

public class ESP_start {}

Here is the same question which I posted above: How can I create suite from separate tests which are using different activities and rules, how it should look like?

Answer

Just in case, maybe answer will be useful for someone else, this suite is correct, previously I have used parameters in my test-classes. In this case app test-classes should be void without any params:

@RunWith(Suite.class)
@Suite.SuiteClasses(
    {
            ESP_test1.class,
            ESP_test2.class,
            ESP_test3.class
    })

public class ESP_start {} 


Source: stackoverflow