UI Test for custom android datepicker Library

Viewed 162

I'm using this library in my android project :

implementation 'com.github.florent37:singledateandtimepicker:2.2.7'

but when I'm doing UI testing with Espresso but when use it like that :

  onView(withClassName(Matchers.equalTo(SingleDateAndTimePicker::class.java.name))).perform(
            PickerActions.setDate(2021, 6, 30)
        )

or like that

onView(withId(R.id.picker)).perform(PickerActions.setDate(2021, 7, 15))

it gives me this error

androidx.test.espresso.PerformException: Error performing 'set date' on view 'with class name: "com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker"'.

or this error

androidx.test.espresso.PerformException: Error performing 'set time' on view 'with id: com.test.testlist:id/picker'.

with this error details :

 Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:(is assignable from class: class android.widget.DatePicker and is displayed on the screen to the user)Target view: "SingleDateAndTimePicker{id=2131230850, res-name=date_time_picker, visibility=VISIBLE, width=1038, height=604, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@666160a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=21.0, y=365.0, child-count=1}"

any solution for that?

1 Answers

You can do use this:

public final class SingleDateAndTimePickerAction {

    private SingleDateAndTimePickerAction() {
        // no Instance
    }

    /** Returns a {@link ViewAction} that sets a date on a {@link com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker}. */
    public static ViewAction setDate(final Date date) {


        return new ViewAction() {

            @Override
            public void perform(UiController uiController, View view) {
                final SingleDateAndTimePicker allInOnePickerDate = (SingleDateAndTimePicker) view;

                allInOnePickerDate.setDefaultDate(date);
            }

            @Override
            public String getDescription() {
                return "set date";
            }

            @SuppressWarnings("unchecked")
            @Override
            public Matcher<View> getConstraints() {
                return allOf(isAssignableFrom(SingleDateAndTimePicker.class), isDisplayed());
            }
        };
    }

}
Related