【问题标题】:Restart activity from ActivityTestRule after failed test测试失败后从 ActivityTestRule 重新启动活动
【发布时间】:2016-07-20 06:09:47
【问题描述】:

我已修改 ActivityTestRule 以重试执行失败的测试用例。每当测试失败时,我需要重新启动活动并重新运行执行。我试过打电话给mActivity.finish(),但目前的活动没有完成也没有重新开始。但测试重新开始。我从here 遵循了这个方法

public class MyActivityTestRule<T extends Activity> extends ActivityTestRule<T> {

private final Class<T> mActivityClass;
private T mActivity;
private static final String TAG = "ActivityInstrumentationRule";
private boolean mInitialTouchMode = false;
private Instrumentation mInstrumentation;


public MyActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
    super(activityClass, initialTouchMode, launchActivity);
    mActivityClass = activityClass;
    mInitialTouchMode = initialTouchMode;
    mInstrumentation = InstrumentationRegistry.getInstrumentation();
}

@Override
public Statement apply(Statement base, Description description) {
    final String testClassName = description.getClassName();
    final String testMethodName = description.getMethodName();
    final Context context =  InstrumentationRegistry.getTargetContext();
   /* android.support.test.espresso.Espresso.setFailureHandler(new FailureHandler() {
        @Override public void handle(Throwable throwable, Matcher<View> matcher) {
            if(AutomationTestCase.mEnableScreenshotOnFailure)
            SpoonScreenshotOnFailure.perform("espresso_assertion_failed", testClassName, testMethodName);
            new DefaultFailureHandler(context).handle(throwable, matcher);
        }
    });*/
     // return super.apply(base, description);
     return statement(base, description);
}

private Statement statement(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            Throwable caughtThrowable = null;
            //retry logic
            for (int i = 0; i < 3; i++) {
                try {
                    launchAppActivity(getActivityIntent());
                    base.evaluate();
                    return;
                } catch (Throwable t) {
                    caughtThrowable = t;
                    System.err.println(description.getDisplayName() + ": run " + (i+1) + " failed");
                    finishActivity();
                } finally {
                    finishActivity();
            }
            }
            System.err.println(description.getDisplayName() + ": giving up after " + 3 + " failures");
            throw caughtThrowable;
        }
    };
}

void finishActivity() {
    if (mActivity != null) {
        mActivity.finish();
        mActivity = null;
    }
}

public T launchAppActivity(@Nullable Intent startIntent) {
    // set initial touch mode
    mInstrumentation.setInTouchMode(mInitialTouchMode);

    final String targetPackage = mInstrumentation.getTargetContext().getPackageName();
    // inject custom intent, if provided
    if (null == startIntent) {
        startIntent = getActivityIntent();
        if (null == startIntent) {
            Log.w(TAG, "getActivityIntent() returned null using default: " +
                    "Intent(Intent.ACTION_MAIN)");
            startIntent = new Intent(Intent.ACTION_MAIN);
        }
    }
    startIntent.setClassName(targetPackage, mActivityClass.getName());
    startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Log.d(TAG, String.format("Launching activity %s",
            mActivityClass.getName()));

    beforeActivityLaunched();
    // The following cast is correct because the activity we're creating is of the same type as
    // the one passed in
    mActivity = mActivityClass.cast(mInstrumentation.startActivitySync(startIntent));

    mInstrumentation.waitForIdleSync();

    afterActivityLaunched();
    return mActivity;
}}

【问题讨论】:

    标签: android junit android-testing android-espresso


    【解决方案1】:

    如果您的测试最终在与 ActivityTestRule 初始化时不同的活动中结束,则您必须关闭后台堆栈中的所有活动。这个例子可能会有所帮助:

    https://gist.github.com/patrickhammond/19e584b90d7aae20f8f4

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 2017-02-15
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2015-07-04
      • 1970-01-01
      • 2014-01-13
      相关资源
      最近更新 更多