Search code examples
android-testingandroid-espressoandroid-instrumentation

How to restart Android app (/app state) within Espresso test?


I'm testing login page - specifically "Autologin" checkbox, so that user being logged-in once, will be automatically logged-in upon reopening the app (by default user should login from scratch).

How can I simulate this behaviour? Is restarting an app is the only way? Can I reset an app somehow to initial screen (as if being restarted), but so that userdata/cookies should be kept?


Solution

  • My initial solution was to close the app by Espresso.pressBackUnconditionally() (it is similar to Espresso.pressBack() but will not throw an exception when Espresso navigates outside the application or process under test) and to launch activity again: activityRule.launchActivity(null).

    However, at the end we came up with more sophisticated solution of relaunching activity within instrumentation:

    with(activityRule) {
        finishActivity()
        launchActivity(null)
    }