【问题标题】:How to make Android TestRunner wait until activity finishes如何让 Android TestRunner 等到活动完成
【发布时间】:2013-11-06 12:16:42
【问题描述】:

我在一个测试用例的末尾关闭一个 Activity,所以它在下一个测试用例的开头为空:

public void test1() throws Exception {

    //activity is calling the RegisterActivity in onCreate
    activity = getActivity();

    // register next activity that need to be monitored.
    RegisterActivity nextActivity = (RegisterActivity) getInstrumentation()
        .waitForMonitorWithTimeout(activityMonitor, 5);
    // next activity is opened and captured.
    assertNotNull(nextActivity);
    if (nextActivity != null) {
        nextActivity.finish();
    }
}

public void test2() throws Exception {
    //nextActivity.finish() from the previous test has not yet finished

    //body of test2
    //...
    //...
}

如果我在 test1 中设置了线程睡眠,那么问题就解决了:

    if (nextActivity != null) {
        nextActivity.finish();
        Thread.sleep(1500);
    }

有没有更好的方法来解决这个问题?有没有一种方法可以阻止 TestRunner 直到 nextActivity 完成?

【问题讨论】:

    标签: android testing wait activity-finish test-runner


    【解决方案1】:

    尝试创建一个new thread 以供下一个活动在其中运行。调用thread.start() 方法后,在您希望阻止TestRunner 的位置调用thread.join()

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2010-11-07
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多