使用Robotium后如何测试我当前的活动

时间:2015-07-27 11:14:07

标签: android unit-testing junit robotium

我对Robotium有一些乏味的问题。通常我用机器人测试时, 如果通过单击FirstActivity中的按钮进入SecondActivity,则可以使用assertCurrentActivity()方法检查Robotium的SecondActivity。

例如,打开一个活动,单击一个按钮,打开B Activity,并且assertCurrentActivity(B)返回true。

Hovewer;如果Applicaiton在A活动中的onCreate方法中启动,则会打开B活动,但无法使用assertCurrentActivity(B)检查Robotium。它说当前的活动仍然是A活动。

MainActivity.class

@Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        selector();
        sharedPrefencesHelper.setAppLaunched( true );
        periodsCount = periodListOrdering.getAllPeriodSize();

        if ( periodsCount < 0 )
            intentCalendarActivity();

        replaceNewFragment( EntryFragment.newInstance( this ), ENTRYPAGE_FRAGMENT_TAG );
    }

intentCalendarActivity()方法发送到CalendarActivity.class  并正确打开CalendarActivity。

我的测试代码

public void testScenario1() {
        // Kilidi aç
        solo.unlockScreen();
        // DB içindek tüm periodları sil.
        myDatabaseQuery.clearPeriodDB();
        // CalendarActivity için ne olur ne olmaz diye bekleme yapalım.
        solo.waitForActivity( "CalendarActivity" );
        // Takvimi doğrula.
        solo.assertCurrentActivity( "CalendarActivity not found", "CalendarActivity" );
        // Hiç bişey yapmadan geri dön.
        solo.goBack();
        // NoDataMessageActivity acitivity e gönderilmelidir.
        solo.waitForActivity( NO_DATA_MESSAGE_ACTIVITY );
        // NoDataMessageActivity gittiğini doğrula
        solo.assertCurrentActivity( NO_DATA_MESSAGE_ACTIVITY + " is not found", NO_DATA_MESSAGE_ACTIVITY );
        // Bir şey yapmadan geri dön
        solo.goBack();
        }

来自Robotium的消息

junit.framework.ComparisonFailure: CalendarActivity not found expected:<[Calendar]Activity> but was:<[Main]Activity>
at com.robotium.solo.Asserter.assertCurrentActivity(Asserter.java:43)
at com.robotium.solo.Solo.assertCurrentActivity(Solo.java:972)
at com.bitbar.recorder.extensions.ExtSolo.assertCurrentActivity(ExtSolo.java:193)
at com.medyasef.she.NoDataActivityTest.testScenario1(NoDataActivityTest.java:54)
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:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

0 个答案:

没有答案