Я использую Espresso для тестирования приложения для Android. У меня возникли проблемы с поиском способа доступа и выбора RadioButton (который принадлежит RadioGroup) текущего действия. У кого-нибудь есть предложения?
Доступ к RadioButton и выбор его в Espresso
comment
Вы что-то пробовали и получили ошибку?
- person Daniel Lubarov   schedule 31.03.2015
comment
@ Даниэль, новичкам вроде меня нужно с чего-то начинать. Эти вопросы — идеальная приманка.
- person appoll   schedule 16.04.2015
Ответы (3)
Учитывая следующую компоновку:
<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>
Напишите новый тестовый метод со следующим:
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())));
Вуаля!
person
appoll
schedule
16.04.2015
для приведенного выше решения, если not
не разрешима, используйте isNotChecked()
вместо 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()));
person
Saurabh Pal
schedule
02.02.2018
У меня была аналогичная проблема с добавленной проблемой, что мои RadioButtons были сгенерированы во время выполнения, поэтому я не мог получить к ним прямой доступ по идентификатору.
Тем не менее, предлагаемое решение также работает при доступе к RadioButtons по их меткам с помощью метода withText
:
onView(withText(R.string.firstButtonLabelStringRes))
.perform(click());
onView(withText(R.string.firstButtonLabelStringRes))
.check(matches(isChecked()));
onView(withText(R.string.secondButtonLabelStringRes))
.check(matches(isNotChecked())));
onView(withText(R.string.thirdButtonLabelStringRes))
.check(matches(isNotChecked())));
Редактировать: я столкнулся с текстами, которые не уникальны, поэтому я решил использовать сопоставитель hamcrest allOf
:
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
...
onView(allOf(withText(R.string.buttonLabelStringRes),
withParent(withId(R.id.radioGroupId))))
.check(matches(isChecked()));
person
Aldinjo
schedule
28.06.2019