Search code examples
xamarinuitest

Xamarin forms changing time picker value for UI test


In Xamarin forms for the time picker below

enter image description here

How do I change the time picker value for the xamarin UI test. I can open the time picker but not sure how to change the time. This is a screen shot of the app.Repl() tree for the time picker.

enter image description here

This is my attempt for trying to change the hours.

app.WaitForElement(c => c.Marked("hours"));
app.Tap(c => c.Marked("hours"));
app.EnterText(c => c.Marked("hours"), newHours) // errors can't pull up keyboard

Solution

  • I found the easiest way to handle date and time controls was to use the Invoke method to access the backdoors that are exposed in the time/date picker controls.

    // invokes a method to input the hour into the picker, time format is 24 hour
    app.Query(c => c.Class("RadialTimePickerView").Invoke("setCurrentHour", (int)hour));
    
    // invokes a method to input the minute into the picker
    app.Query(c => c.Class("RadialTimePickerView").Invoke("setCurrentMinute", (int)minute));
    

    And the date picker is very similar, but instead of requiring two separate methods, it just uses one.

    // invokes a method to input the date into the picker, where month starts from 0
    app.Query(c => c.Raw("DatePicker").Invoke("updateDate", (int)year, (int)month - 1, (int)day));
    

    I would recommend using this particular backdoor as it works across a wide variety of android version, as each version of android has a slightly different style of date/time picker.