Automate number selection in android with espresso

How to automate a number picker using espresso. I want to set a specific time in timePicker using espresso.

+8
source share
5 answers

To match a view by its class name, you can simply use:

onView(withClassName(Matchers.equalTo(TimePicker.class.getName()))); 

Once you have a ViewInteraction object, you can set a value for it that defines and uses ViewAction, as shown below:

 public static ViewAction setTime(final int hour, final int minute) { return new ViewAction() { @Override public void perform(UiController uiController, View view) { TimePicker tp = (TimePicker) view; tp.setCurrentHour(hour); tp.setCurrentMinute(minute) } @Override public String getDescription() { return "Set the passed time into the TimePicker"; } @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(TimePicker.class); } }; } 
+15
source

Align the view and then do the action:

 ViewInteraction numPicker = onView(withClassName(Matchers.equalTo(NumberPicker.class.getName()))); numPicker.perform(setNumber(1)); 

Create a ViewAction to set the number:

 public static ViewAction setNumber(final int num) { return new ViewAction() { @Override public void perform(UiController uiController, View view) { NumberPicker np = (NumberPicker) view; np.setValue(num); } @Override public String getDescription() { return "Set the passed number into the NumberPicker"; } @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(NumberPicker.class); } }; } 
+10
source

For those who consider this question later (like me), this may be useful: DateTimePickerTest uses PickerActions . PickerActions allows code for such date pickers (Java):

 onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month + 1, day)); 

Or for time collectors (Kotlin):

 onView(withClassName(Matchers.equalTo(TimePicker::class.java.name))).perform(PickerActions.setTime(0, 10)) 
0
source

There is a problem with the accepted answer: it does not work when the event changes. Therefore (if you need it) you cannot check if your view reacts to this when an event changes.

The following code (kotlin) is still not cool, but I think this is the only way.

 fun setValue(value: Int): ViewAction { return object : ViewAction { override fun getDescription(): String { return "set the value of a " + NumberPicker::class.java.name } override fun getConstraints(): Matcher<View> { return ViewMatchers.isAssignableFrom(NumberPicker::class.java) } // the only way to fire onChange event is to call this private method override fun perform(uiController: UiController?, view: View?) { val numberPicker = view as NumberPicker val setValueMethod = NumberPicker::class.java.getDeclaredMethod( "setValueInternal", Int::class.java, Boolean::class.java ) setValueMethod.isAccessible = true setValueMethod.invoke(numberPicker, value, true) } } } 
0
source

I found a solution to this problem: the Luigi Massa Gallerano solution does not call the listener for changes.

In addition to its method, you need to add a wrap method that swipes your finger up and down each time. This causes changeListener, although the former value is, of course, lost.

 fun setNumberPickerValue(viewInteraction: ViewInteraction, value: Int) { viewInteraction.perform(setValue(value)) viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_CENTER, GeneralLocation.BOTTOM_CENTER, Press.FINGER)) SystemClock.sleep(50) viewInteraction.perform(GeneralSwipeAction(Swipe.SLOW, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER)) SystemClock.sleep(50) } 

In the above example, NumberPicker and Kotlin are used, but in principle they are one and the same.

0
source

Source: https://habr.com/ru/post/970429/


All Articles