Android Honeycomb +中的Robotium测试

时间:2014-08-14 12:28:37

标签: android robotium

我刚开始使用Robotium并编写了一个简单的测试:

public class TestLoginActivity extends ActivityInstrumentationTestCase2<LoginActivity> {
    private static final String LOG = "TestLoginActivity";
    private Solo solo;

    public TestLoginActivity() {
        super(LoginActivity.class);
    }

    protected void setUp() throws Exception {
        super.setUp();

        solo = new Solo(new Instrumentation());
    }

    public void testPreferences(){
        Log.e(LOG, "0");
        getActivity();
        Log.e(LOG, "1 " + solo.getCurrentActivity().getClass().getName());
        solo.assertCurrentActivity("Check login activity", LoginActivity.class);
        Log.e(LOG, "2");
        solo.waitForActivity(LoginActivity.class);
        Log.e(LOG, "3");
        solo.clickOnView(solo.getCurrentActivity().findViewById(com.package.R.id.ic_settings));
        Log.e(LOG, "4");
        solo.waitForActivity(PrefsActivity.class);
        Log.e(LOG, "5");
        Log.e(LOG, solo.getCurrentActivity().getClass().getName());
        Log.e(LOG, "6");
        solo.assertCurrentActivity("Check " + solo.getCurrentActivity().getClass().getSimpleName(), PrefsActivity.class);
    }
}

我正在使用构造函数Solo(Instrumentation instrumentation),因为Solo(Instrumentation instrumentation, Activity activity)方法solo.getCurrentActivity()会返回上一个活动。问题是,现在getActivity()没有任何反应。等待30秒,然后测试失败 重要的是我必须使用Android 3.0 +

1 个答案:

答案 0 :(得分:3)

删除你的setUp代码并使用以下内容,没有拆解部分,getActivity()将返回错误的活动。

@Override
public void setUp() throws Exception {
    super.setUp()
    solo = new Solo(getInstrumentation(), getActivity());
}
@Override
protected void tearDown() throws Exception {
    solo.finishOpenedActivities();
    super.tearDown();
}