How to test android app has closed with Espresso
Asked Answered
G

1

1

How do I test if an Android application has closed after I click the native back button?

I can test if an activity has opened, but how do I test if a app has closed?

Graviton answered 11/4, 2016 at 5:37 Comment(0)
H
0

I'm guessing you might be able to do it through the Instrumentation class. The idea being, if you are not able to come back to the app from the last activity from which you exited, then the app is closed. I haven't tested it but perhaps you could do something like this:

Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
Instrumentation.ActivityMonitor activityMonitor = instrumentation.addMonitor(LastActivity.class.getName(), null, false);
Activity activity = instrumentation.waitForMonitorWithTimeout(activityMonitor, 1000);

Espresso.pressBack();

if(activity != null) {
  // do something
  fail();
}
Herzig answered 14/9, 2016 at 7:35 Comment(1)
But if we use LastActivity.class.getName(), this is actually another activity itself and not a finish state of the first activity.Broek

© 2022 - 2024 — McMap. All rights reserved.