我正在尝试使用Espresso for
Android进行一系列测试.运行之间似乎没有关闭活动.一次测试后,无论应用程序状态如何,左侧为下一次测试.
如何用Espresso来实现?
错误报告中提供的修复程序的问题是,这将仅在整个套件的完成时执行.如果你想在每次测试后都有一个干净的活动堆栈,你需要手动做一些事情.我写了一个类似于上面提到的机票的修复程序,但可以在任何时间点执行.
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitorRegistry;
import com.google.android.apps.common.testing.testrunner.Stage;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public final class ActivityFinisher implements Runnable {
public static void finishOpenActivities() {
new Handler(Looper.getMainLooper()).post(new ActivityFinisher());
}
private ActivityLifecycleMonitor activityLifecycleMonitor;
public ActivityFinisher() {
this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
}
@Override
public void run() {
final List activities = new ArrayList();
for (final Stage stage : EnumSet.range(Stage.CREATED,Stage.STOPPED)) {
activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
}
for (final Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}