How to test android app has closed with Espresso

拈花ヽ惹草 提交于 2019-12-13 01:38:50

问题


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?


回答1:


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();
}


来源:https://stackoverflow.com/questions/36540549/how-to-test-android-app-has-closed-with-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!