dimanche 8 mai 2016

How to prevent activity to be launched during each test using ActivityInstrumentationTestCase2

I have a class that intends to test the app UI, however, each time that a new method annotated with @Test starts the activity is launched again - I would like to launch it once and performs the tests in the launched instance. Is there a way to do that?

Test class example:

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestActivityHomepage extends ActivityInstrumentationTestCase2 <ActivityHomepage> {

    private static final String TAG = "TestActivityHomepage";


    private ActivityHomepage mActivity;
    public ActivityTestRule<ActivityHomepage> rule = new ActivityTestRule(ActivityHomepage.class, true, false);

    public TestActivityHomepage() {
        super(ActivityHomepage.class);
    }

   @Before
    public void setUp() throws Exception {
        super.setUp();
        Intent intent = new Intent();
        mActivity = rule.launchActivity(intent);
    }

    @Test
    public void testPreconditions() {
        assertNotNull(mActivity);
    }

    @Test
    public void testLoadMoreCharacters () throws InterruptedException {
        RecyclerView characters = (RecyclerView) this.mActivity.findViewById(R.id.characters);
        int previousSize = characters.getAdapter().getItemCount();
        characters.smoothScrollToPosition(previousSize);
        Thread.sleep(2000);
        int newSize = characters.getAdapter().getItemCount();
        onView(withId(R.id.characters)).perform(RecyclerViewActions.scrollToPosition(previousSize));
        assertNotSame(previousSize, newSize);
    }

    @Test
    public void openActivityCharacter () {
        onView(withId(R.id.characters))
                .perform(
                        RecyclerViewActions
                                .actionOnItemAtPosition(0, click()));

        assertFalse(mActivity.isRunning());
    }
}

Aucun commentaire:

Enregistrer un commentaire