Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a specific rating on RatingBar in Espresso?

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.

like image 346
Eduard Avatar asked Aug 08 '14 17:08

Eduard


2 Answers

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.

like image 142
yogurtearl Avatar answered Oct 11 '22 14:10

yogurtearl


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);
    }
}
like image 26
appoll Avatar answered Oct 11 '22 12:10

appoll