找不到ImageButtons Robotium Android

时间:2014-02-12 08:12:06

标签: android imagebutton robotium android-testing

MainActivityFunctionalTest

if (solo.waitForActivity(MainActivity.class)) {
assertTrue(solo.waitForView(R.id.btn_next));
             ImageButton submitBtn = (ImageButton)
             solo.getImageButton(R.id.btn_next);
assertNotNull(submitBtn);
            solo.clickOnImageButton(R.id.btn_next);
}

由于以下异常导致测试用例失败:

02-12 15:59:11.612: I/TestRunner(3010): junit.framework.AssertionFailedError: 2131361828 ImageButtons are not found!
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.Assert.fail(Assert.java:50)
02-12 15:59:11.612: I/TestRunner(3010):     at com.robotium.solo.Waiter.waitForAndGetView(Waiter.java:513)
02-12 15:59:11.612: I/TestRunner(3010):     at com.robotium.solo.Getter.getView(Getter.java:50)
02-12 15:59:11.612: I/TestRunner(3010):     at com.robotium.solo.Solo.getImageButton(Solo.java:1850)
02-12 15:59:11.612: I/TestRunner(3010):     at com.morpho.morphonfclek.test.MainActivityFunctionalTest.testRegistrationFormIsSubmitted(MainActivityFunctionalTest.java:110)
02-12 15:59:11.612: I/TestRunner(3010):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 15:59:11.612: I/TestRunner(3010):     at java.lang.reflect.Method.invoke(Method.java:525)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.TestCase.runBare(TestCase.java:134)
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.TestResult$1.protect(TestResult.java:115)
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.TestResult.runProtected(TestResult.java:133)
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.TestResult.run(TestResult.java:118)
02-12 15:59:11.612: I/TestRunner(3010):     at junit.framework.TestCase.run(TestCase.java:124)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
02-12 15:59:11.612: I/TestRunner(3010):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
02-12 15:59:11.612: I/TestRunner(3010):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)

我也尝试了以下操作并遇到了同样的错误:

ImageButton submitBtn = (ImageButton)
             solo.getView(R.id.btn_next);  

以及以下内容:

ImageButton submitBtn = (ImageButton)
             mainActivity.findViewById(R.id.btn_next);

以及以下内容:

View submitBtn = solo.getView(R.id.btn_next);

但问题仍然存在。

请注意,assertNotNull(submitBtn);不会导致失败==>按钮实际上就在那里!

布局如下:
registration.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Step1Layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include android:id="@+id/layout_navibar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/layout_headbar"
        layout="@layout/navibar" />
</RelativeLayout>

navibar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="#cc0000" >

    <ImageButton
        android:id="@+id/btn_back"
        android:contentDescription="@string/btn_back"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_navi_home" 
        android:visibility="invisible" />


    <ImageButton
        android:id="@+id/btn_next"
        android:contentDescription="@string/btn_next"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_navi_next" />

    <TextView
        android:id="@+id/label_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

solo.getImageButton(int index)是ImageButton的索引,而不是资源ID。有关更多信息,请参阅Robotium Javadoc。