Skip to main content

Pick Date/Time in Xamarin UITest

·2 mins

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.

Pick date on iOS

Pick date on iOS

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 richseviora. By the way, there is a gist for a mean teriyaki sauce on his account as well, but I’m drifting off.

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.

Summary #

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 buildpipeline, you could have a great start reading this post here. Although the post is quite old, you will get the idea.