For the past few weeks, I have been busy writing automated UI tests for a Xamarin.Forms app. At some point, I ran into the limitation that there was no apparent way to pick date and times. This post will provide you with the code to do just that. Possibly this post is more a “note to self” than an actual post…
The Date Picker Control
For our app, we implemented a simple date picker. In Xamarin.Forms, this translates to an input field which, when tapped, shows the platform native control to pick a date. You can see it in action on iOS in the screenshot underneath.
However, because a native control is spawned to let you pick the date (or time for that matter), you do not have any control over how to access that control programmatically. You could come up with all kinds of workarounds like doing something with coordinates, but that feels yucky.
Pick Date/Time Extension Methods
One of the top hits in Google was a GitHub gist by
This gist, which you can see embedded underneath, gives you a couple of methods that simply allow you to pick a date and a time. The method signatures for UpdateDatePicker and UpdateTimePicker are quite simple.
Since they are extension methods, you will call them on the IApp object, which represents the app you are running the tests on. The second parameter takes in the date of time you want to select. The last two parameters can be ignored unless you know what you’re doing.
You will probably just want to take this code and copy/paste it into your project and use it for the better. I would recommend to read through it and try to understand what is going on here. It might benefit you later on if you need to fix something, or maybe another scenario comes up where you have to do something similar.
If you want to know more about Xamarin UITest, please let me know, I’ll be happy to write a post about it. Or maybe you want to incorporate it into your
4 thoughts on “Pick Date/Time in Xamarin UITest”
Thank you for all the contribution to the community. I want to get started with Xamarin.UITests. It will be very helpful if you can post on that. Integrating test, writing test and testing on device. Complete tutorial. May be on simple Page but complete procedure.
Thank you for reading! I am working on a UITest introduction post. In the meantime you may want to start here: https://www.jimbobbennett.io/ui-testing-your-xamarin-apps/
Thanks for the link. The link have some bad characters in it. the actual link is:
Hm, don’t know how that happened… But that’s the one, yes!
Comments are closed.