All Collections
Test
Controlling DatePickers in Xamarin.Forms apps with Xamarin.UITest on iOS
Controlling DatePickers in Xamarin.Forms apps with Xamarin.UITest on iOS
Jihye E avatar
Written by Jihye E
Updated over a week ago

Users often have trouble selecting values from DatePickers in UITest – this issue really stems from the fact that the DatePicker control on iOS is not part of your app but an OS construct which sits outside of your app’s UI interface. 

In the past – it's been possible to interact with these elements by setting the values in code as follows: 

app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Month - 1, "inComponent", 0, "animated", true)); 

app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Day - 1, "inComponent", 1, "animated", true));

app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Year - 1, "inComponent", 2, "animated", true)); 

However in recent versions of iOS selecting values on the DatePicker in this way does not trigger the DatePicker delegate methods so views that respond to date pages are not updated. 

As a workaround – we suggest nudging each control very slightly to trigger the label to update: 

app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Month - 1, "inComponent", 0, "animated", true)); 
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Day - 1, "inComponent", 1, "animated", true));
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Year - 1, "inComponent", 2, "animated", true));  

var rect = app.Query(x => x.Class("UIPickerTableView").Index(0).Child()).First().Rect;
app.DragCoordinates(rect.CenterX, rect.CenterY, rect.CenterX, rect.CenterY + 20);  

rect = app.Query(x => x.Class("UIPickerTableView").Index(3).Child()).First().Rect;
app.DragCoordinates(rect.CenterX, rect.CenterY, rect.CenterX, rect.CenterY + 20);  

rect = app.Query(x => x.Class("UIPickerTableView").Index(6).Child()).First().Rect;
app.DragCoordinates(rect.CenterX, rect.CenterY, rect.CenterX, rect.CenterY + 20);

app.Tap(c => c.Text("Done")); 

We’ve tested this approach on the following devices in App Center Test using the Xamarin.Forms Gallery app and verified that it works as expected on devices running iOS 8.4.1 through 11.4.1. 

Did this answer your question?