1

我使用robotium 3.5.1在我的android程序中创建了简单的测试,代码如下:

protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}

protected void tearDown() throws Exception {
    solo.finishOpenedActivities();
}

public void testAddTabs() throws Exception {


    addTab("TAB1");
    addPreset("TAB1");
    addTab("TAB2");
    addPreset("TAB2");


}

public void addTab(String tabName) throws Exception {

    solo.assertCurrentActivity("Checking current Activity",
            MainActivity.class);
    Log.d("MyTag", solo.getCurrentActivity().getLocalClassName() );
    solo.clickOnView(solo.getView(com.sm.scam.R.id.menu_new_tab));
    solo.enterText(0, tabName);
    solo.clickOnText("OK");

}

public void addPreset(String name) throws Exception {
    Log.d("MyTag", solo.getCurrentActivity().getLocalClassName() );
    solo.goBackToActivity("MainActivity");
    Log.d("MyTag", solo.getCurrentActivity().getLocalClassName() );
    solo.clickOnText(name);
    solo.clickOnText("New Preset");
    Log.d("MyTag", solo.getCurrentActivity().getLocalClassName() );
    solo.assertCurrentActivity("Checking current Activity",
            PresetActivity.class);

    String presetName = "Moti" + 1;
    solo.enterText(0, presetName);
    solo.pressSpinnerItem(0, 1);
    solo.clickOnText("Darkened");
    solo.clickOnText("GPS Tag");
    solo.clickOnView(solo.getView(com.sm.scam.R.id.menu_save_preset));
}

它只需添加新选项卡,然后在选项卡中按下按钮,更改编辑文本、微调器和 2 个切换按钮并保存。在第一个选项卡中一切正常,但在第二个选项卡中,以下代码:

solo.clickOnText("New Preset");

只是不要什么都不做!单击此按钮时没有任何反应,它应该转发到 PresetActivity,因为它在第一个选项卡中正确转发。

错误代码:

junit.framework.ComparisonFailure: Checking current Activity expected:<...Preset...>            but was:<...Main...>
at com.jayway.android.robotium.solo.Asserter.assertCurrentActivity(Asserter.java:57)
at com.jayway.android.robotium.solo.Solo.assertCurrentActivity(Solo.java:570)
at com.sm.scam.test.ScamTest.addPreset(ScamTest.java:55)
at com.sm.scam.test.ScamTest.testAddTabs(ScamTest.java:32)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at                                                                                                                                                                        android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java   :186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1574)

我也试图通过调用来强制它:

solo.goBackToActivity("MainActivity");

但仍然没有。如果有人帮助我,将不胜感激。谢谢

4

2 回答 2

1

TabHost? if so that is probably your problem. TabHosts involve having multiple activities open and this really confuses robotium. That is because robotium only knows about the last activity that it saw, this means in a scenario where multiple activities are open then the one it sees last might not be the one you are expecting it to be.

You can see this is happening because it isn't failing to click on anything it is in fact just on the wrong activity and thats the assertion that fails.

于 2012-11-15T23:03:21.570 回答
0

This is because sometimes assert executes before your activity was loaded. So try to use before assertActivity something like:

solo.waitForActivity(getCurrentActivity().getClass().getSimpleName());
solo.assertCurrentActivity(PresetActivity.class);
于 2012-11-22T20:01:37.407 回答