I am using Espresso to test an Android application. I am having trouble trying to find a way to access and select a RadioButton (which belongs to a RadioGroup) of the current Activity. Does anyone have any suggestions?
Given the following layout:
<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>
Write a new test method with the following:
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())));
Voila!
for the above solution, if not
is not resolvable use isNotChecked()
in place of not(isChecked())
onView(withId(R.id.firstRadioButton))
.perform(click());
onView(withId(R.id.firstRadioButton))
.check(matches(isNotChecked()));
onView(withId(R.id.secondRadioButton))
.check(matches(isNotChecked())));
onView(withId(R.id.thirdRadioButton))
.check(matches(isNotChecked()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With