I'm trying to write an Espresso test that tests a RatingBar selection. How can I set a specific rating using Espresso? I only see click()
, which always sets the middle rating.
You need to create a custom subclass of ViewAction that calls setRating on the view, then pass an instance of your custom view action to the perform() method.
The accepted answer helped me write the following custom ViewAction:
public final class SetRating implements ViewAction {
@Override
public Matcher<View> getConstraints() {
Matcher <View> isRatingBarConstraint = ViewMatchers.isAssignableFrom(RatingBar.class);
return isRatingBarConstraint;
}
@Override
public String getDescription() {
return "Custom view action to set rating.";
}
@Override
public void perform(UiController uiController, View view) {
RatingBar ratingBar = (RatingBar) view;
ratingBar.setRating(3);
}
}
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