2013-04-05 99 views
4

在我的Android應用程序視圖佈局,我有一個<RadioGroup>其中包含兩個<RadioButton>(Robotium)如何在RadioGroup中選擇一個單選按鈕

<RadioGroup 
    android:id="@+id/my_radio_group" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

     <RadioButton 
     android:id="@+id/yes_btn" 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/yes" 
     /> 

    <RadioButton 
     android:id="@+id/no_btn" 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/no" 
     /> 
</RadioGroup> 

我用Robotium庫編寫JUnit測試爲此收音機組選擇一個單選按鈕。測試代碼片段是:

Solo solo = new Solo(getInstrumentation(), getActivity()); 
... 
solo.clickOnRadioButton(R.id.yes_btn); //I expect the "yes" radio button will be selected 

我希望上面的測試代碼會選擇「是」單選按鈕,但是,當運行它,它拋出一個錯誤

junit.framework.AssertionFailedError: 2131427675 RadioButtons are not found! 
at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:417) 
at com.jayway.android.robotium.solo.Clicker.clickOn(Clicker.java:374) 
at com.jayway.android.robotium.solo.Solo.clickOnRadioButton(Solo.java:1063) 
... 

哪有我在<RadioGroup>中選擇一個<RadioButton>然後用Robotium ??

回答

2

嘗試下面代碼

RadioButton rb = (RadioButton) solo.getView(R.id.yes_btn); 
solo.clickOnView(rb); 

solo.clickOnRadioButton()採用視圖索引作爲參數,而要傳遞資源ID。

+0

謝謝,它的工作原理! – Mellon 2013-04-05 13:46:32

相關問題