访问RadioButton并在Espresso中选择它

And*_* W. 6 android android-espresso

我正在使用Espresso来测试Android应用程序.我无法找到访问和选择当前Activity的RadioButton(属于RadioGroup)的方法.有没有人有什么建议?谢谢

-安德鲁

app*_*oll 5

鉴于以下布局:

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <RadioButton
        android:id="@+id/firstRadioButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/first_radio_button" />

    <RadioButton
        android:id="@+id/secondRadioButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/second_radio_button" />

    <RadioButton
        android:id="@+id/thirdRadioButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/third_radio_button" />

</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

使用以下内容编写新的测试方法:

onView(withId(R.id.firstRadioButton))
    .perform(click());
    
onView(withId(R.id.firstRadioButton))
    .check(matches(isChecked()));
    
onView(withId(R.id.secondRadioButton))
    .check(matches(not(isChecked())));
    
onView(withId(R.id.thirdRadioButton))
    .check(matches(not(isChecked())));
Run Code Online (Sandbox Code Playgroud)

瞧!